INTERVIEW
Javascript
1. What is the difference between let,var, and const?
In JavaScript,
The difference between Call, Apply and Bind can be explained with below examples.
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 withvar
is available throughout the function.let
: Block-scoped. A variable declared withlet
is confined to the block in which it is defined.const
: Block-scoped. Similar tolet
, a variable declared withconst
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 asundefined
.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.
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 |
Angular
1. What are lifecycle hooks available?
- ngOnChanges()
- ngOnInit()
- ngDoCheck()
- ngAfterContentInit()
- ngAfterContentChecked()
- ngAfterViewInit()
- ngAfterViewChecked()
- ngOnDestroy()