The difference between var, let and const

Posted by flatpooks on Wed, 15 Dec 2021 20:09:29 +0100

The difference between var, let and const

General differences:

const defined variables cannot be modified and must be initialized.

Variables defined by var can be modified. If they are not initialized, undefined will be output and no error will be reported. Variables defined by var have no block concept and can be accessed across favorites rather than functions.

Let is a block level scope. The use of let definition inside a function has no impact on the outside of the function. The definition of let can only be accessed in the block scope. It must be initialized and assigned when used. It can only be accessed in the block scope and cannot be modified.

1. Variable procedure declared by var

consloe.log(a);//The output result is undefined
var a = 'xu';
consloe.log(a);//The output result is xu
​The process is as follows:

1. The variable declared by VAR will be promoted to the top to create the a variable

​ 2. After creating the a variable, it will be initialized to undefined

​ 3. Start code execution

​ 4. Assign variable value to a variable

Why output the variable as undefined before var declaration?

2. The process of declaring variables through let

let a = 'xu1'
{
    consloe.log(a);//Throw error uncaught referenceerror: cannot access' a 'before initialization
    let a = 'xu2';
    a = 'xu3'
}
let a = 'xu4'//Uncaught SyntaxError: Identifier 'a' has already been declared

1. Output result: unable to access' a 'before initialization.

2. Output result: identifier "a" has been declared

After error 1 So the question is, will let improve?

The process is as follows:

​ 1.let declared variables are promoted during creation

​ 2. Code execution started without initializing the Temporary dead zone

​ 3. setup code

​ 4. Assign values on the premise of modifying variable values

This is why the output is that 'a' cannot be accessed before initialization
Error 2 Using let to declare variables cannot declare duplicate variables, so an error is thrown

The same variable cannot be declared again within the scope declared by. Otherwise, an error will be thrown

{
    let xu1 = '1';
    let xu1 = '1'; //Error thrown: identifier 'xu1' has already been declared
}

The thrown error indicates that xu1 has been declared. The declared meaning cannot be repeated.

3. Constants declared by const

const a = 123;
console.log(a);
a = 321;//Throw error Uncaught TypeError: Assignment to constant variable

The output is: the constant is reassigned. That is, you modify the constant value declared by const again

Const differs from let only in that const declares constants in two steps:

1.Create and initialize
2.Assignment process blocked
 The assignment cannot be given because the assignment process is blocked const The declared constant is reassigned, which is why an error is thrown after reassigning the constant

What is the constant declared by const?

> Constants, as the name suggests, are commonly used variables. In the case of judging logic, we sometimes need a fixed value as the judgment condition, so we will give priority to using it const Declare constants, and const The declared constant prevents the modification of this quantity, that is to say
> const A declared quantity can only modify its value, so it is called a constant.

4.const and let have block level scope

{
   var a = 'xu1';
   let b = 'xu2';
   const c = 'xu3';
}
	console.log(a); //Output result xu1
	console.log(b); //Throw error ReferenceError: b is not defined
	console.log(c); //Throw error ReferenceError: c is not defined

It can be seen that the constants / variables declared by const and let are limited to the current scope and cannot be accessed in the external scope,
Then another question arises. Do the constants / variables declared by both of them belong to window?

let a = 'xu1'
const b = 'xu2'
var c = 'xu3'
console.log(window.a)//undefined
console.log(window.b)//undefined
console.log(window.c)//xu3

After reading the results, we found another difference between them. Variables declared with or without var in the global scope belong to window, but let and const do not

SE6 has changed the other two ways of declaring variables based on var in the case of global pollution, redundant variables and chaotic declarations of variables. Different declaration methods can be used according to different logical code blocks. When declaring private variables within the scope, variables can be declared through let. When a token that will not be changed is required, Constants can be declared through const. When this variable is needed in a code block, variables can be declared through var, so that they can be accessed globally.

End~

Topics: Javascript Front-end ECMAScript Interview