The answer the interviewer wants most

Var, let, const var, let, const var, let, const

Var is a function-level scope. Let is a block-level scope. For example, variables defined by var in a for loop can be accessed outside of the for loop, while variables defined by let cannot be accessed outside the for loop. The second difference is that let cannot access the variable before it is defined, whereas var can, with its initial value undefined. The third difference is that let can not be defined repeatedly, and will report errors. Var can be defined repeatedly. Const may not be reassigned. Let may be reassigned, but only to basic types such as String, number, or Boolean. When const is an object, we can add or assign the key of the object. When variables are arrays, push and pop operations can also be performed. You can also use object.freeze to keep variables unchanged, but only in a single layer, invalidating nested objects or arrays in the inner layer.

The answer to parse

Var and let:

1. Different scope: var is function scope, let is block scope. Var is declared in a function and is valid throughout the function. Since let is a block scope, variables defined in the block scope, say, in the for loop, are not accessible outside of it, so let is recommended for the for loop.

2. Let cannot access this variable before definition, but var can. Let must be declared before being used. The value of var is undefined if it is used directly but not defined.

This is because the variables declared by var and let are promoted at different stages when promoted.

JS variables are created in three stages: create, initialize, and assign.

  • Create: Create memory space for variable A
  • Initialization: a=undefined
  • Assignment: a = 1

And:

  • The “creation” process of the LET has been improved, but initialization has not. (Many people call this a transient dead zone for LETS.)
  • The “creation” of const is improved, but the initialization is not.
  • Var “creation” and “initialization” have been improved.

3. Let cannot be redeclared in the same variable, but var does

const

Const and let are very similar (they have a lot in common compared to var), but they have one major difference: Let can be reassigned, while const cannot. Therefore, a variable defined by const can have only one value, and that value is assigned when it is declared. So let’s look at the following example.

But is const completely immutable?

Is not!

If a const variable has an array or object as its value, it can change its value like this.

const myConstObject = {mutableProperty: 2};

myConstObject = {};// TypeError
myConstObject.mutableProperty = 3; //ok
console.log(myConstObject.mutableProperty); / / 3
const myConstArray = [1];

myConstArray = []; // TypeError
myConstArray.push(2) //ok
console.log(myConstArray); / / [1, 2]
Copy the code

Of course you can’t use values of primitive data types, such as String, number, Boolean, etc., because they are immutable by nature.

If you want our variables to be truly immutable, you can use object.freeze (), which will keep your objects immutable. Unfortunately, it’s only shallow immutable, and it’s still mutable if you have an object nested within it.

const myConstNestedObject = {
  immutableObject: {
    mutableProperty: 1}};Object.freeze(myConstNestedObject);

myConstNestedObject.immutableObject = 10; // won't change
console.log(myConstNestedObject.immutableObject); // {mutableProperty: 1}
myConstNestedObject.immutableObject.mutableProperty = 10; // ok
console.log(myConstNestedObject.immutableObject.mutableProperty); / / 10

Copy the code