Interview Questions And Answer

INTERVIEW

1. What is the difference between let,var, and const?
In JavaScript, var, let, and const are keywords used to declare variables, each with distinct behaviors and use cases. Here's a precise comparison:
1. Scope
  • var: Function-scoped. A variable declared with var is available throughout the function.
  • let: Block-scoped. A variable declared with let is confined to the block in which it is defined.
  • const: Block-scoped. Similar to let, a variable declared with const is also confined to the block in which it is defined.
2. Hoisting
  • var: Variables are hoisted to the top of their function and initialized as undefined.
  • let: Variables are hoisted but not initialized, leading to a "temporal dead zone" until the declaration is encountered.
  • const: Variables are hoisted but not initialized, also leading to a "temporal dead zone" until the declaration is encountered.
3. Reassignment
  • var: Can be reassigned.
  • let: Can be reassigned.
  • const: Cannot be reassigned after initial assignment.
4. Redeclaration
  • var: Can be redeclared within the same scope.
  • let: Cannot be redeclared within the same scope.
  • const: Cannot be redeclared within the same scope.
5. Initialization Requirement
  • var: Initialization at the time of declaration is optional.
  • let: Initialization at the time of declaration is optional.
  • const: Must be initialized at the time of declaration.
2. What is the difference between Call, Apply and Bind?
  The difference between Call, Apply and Bind can be explained with below examples.

Call: The call() method invokes a function with a given this value and arguments provided one by one.

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?
Apply: Invokes the function with a given this value and allows you to pass in arguments as an array.  
var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?

bind: returns a new function, allowing you to pass any number of arguments

var employee1 = { firstName: "John", lastName: "Rodson" };
var employee2 = { firstName: "Jimmy", lastName: "Baily" };

function invite(greeting1, greeting2) {
  console.log(
    greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
  );
}

var inviteEmployee1 = invite.bind(employee1);
var inviteEmployee2 = invite.bind(employee2);
inviteEmployee1("Hello", "How are you?"); // Hello John Rodson, How are you?
inviteEmployee2("Hello", "How are you?"); // Hello Jimmy Baily, How are you?
3. What is the difference between slice and splice?

Some of the major difference in a tabular form

Slice Splice
Doesn't modify the original array(immutable) Modifies the original array(mutable)
Returns the subset of original array Returns the deleted elements as array
Used to pick the elements from array Used to insert or delete elements to/from array
 

1. What are lifecycle hooks available?
  • ngOnChanges()
Called when an input property changes. Receives a SimpleChanges object that contains the current and previous values of the input properties.
  • ngOnInit()
Called once, after the first ngOnChanges(). Used for component initialization, such as fetching data.
  • ngDoCheck()
Called during every change detection run. Allows developers to implement custom change detection.
  • ngAfterContentInit()
Called once after the first ngDoCheck(). Called after Angular projects external content into the component's view (via ng-content).
  • ngAfterContentChecked()
Called after every ngDoCheck(). Called after Angular checks the content projected into the component.
  • ngAfterViewInit()
Called once after the first ngAfterContentChecked(). Called after Angular initializes the component's views and child views.
  • ngAfterViewChecked()
Called after every ngAfterViewInit(). Called after Angular checks the component's views and child views.
  • ngOnDestroy()
Called just before Angular destroys the component. Used for cleanup, such as unsubscribing from observables and detaching event handlers to avoid memory leaks. Each of these hooks is optional, and developers can implement only those that are needed for their specific use case.
Back to top button