Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
var, let, and const are keywords used to declare variables, each with distinct behaviors and use cases. Here's a precise comparison: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.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.var: Can be reassigned.let: Can be reassigned.const: Cannot be reassigned after initial assignment.var: Can be redeclared within the same scope.let: Cannot be redeclared within the same scope.const: Cannot be redeclared within the same scope.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.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 |