1. Variables declared by var will be mounted on window, while variables declared by let and const will not:
var a = 100; (a,); // 100 100 let b = 10; (b,); // 10 undefined const c = 1; (c,); // 1 undefined
2. Var declares that variables have variable improvements, let and const have variable improvements.
(a); // undefined ===> a has been declared that the value has not been assigned yet, and the undefined value is obtained by defaultvar a = 100; (b); // Error: b is not defined ===> The variable b cannot be foundlet b = 10; (c); // Error: c is not defined ===> The variable c cannot be foundconst c = 10;
3. Let and const declaration form block scope
if(1){ var a = 100; let b = 10; } (a); // 100 (b) // Error: b is not defined ===> The variable b cannot be found if(1){ var a = 100; const c = 1; } (a); // 100 (c) // Report an error:c is not defined ===> Not foundcThis variable
4. Let and const cannot declare variables with the same name under the same scope, while var can
var a = 100; (a); // 100 var a = 10; (a); // 10 let a = 100; let a = 10; // The console error reported:Identifier 'a' has already been declared ===> IdentifieraHas been declared。
5. Temporary dead zone
var a = 100; if(1){ a = 10; //When a is declared using let/const in the current block scope, when a is assigned a value of 10, the variable a will only be found in the current scope. // At this time, it was not yet time to declare it, so the console Error:a is not defined let a = 1; }
6. Const
/* * 1. Once the value is declared, null cannot be used. * * 2. Cannot be modified after declaration * * 3. If the declared compound type data, its properties can be modified * * */ const a = 100; const list = []; list[0] = 10; (list);// [10] const obj = {a:100}; = 'apple'; = 10000; (obj);// {a:10000,name:'apple'}
The above is a detailed explanation of the difference between var and let and const in JavaScript. For more information on the difference between var and let and const in JavaScript, please pay attention to my other related articles!