Home >Web Front-end >JS Tutorial >What are var, let, const in JavaScript? Usage of var, let, const (code)
This chapter will introduce to you what var, let and const are in JavaScript? Usage of var, let, const (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1, var
is a global variable declared in the global window and is an attribute of the global object window.
var sum = 0 console.log(window.sum) // 0 console.log(sum); // 0
The variables declared in the function are local variables
function foo(){ function bar(a){ i = 3; console.log( a + i ); // 3 之后是无限循环11 } var i = 0 for( i ; i <= 10; i++){ bar( i * 2 ) } } foo()
This code can be written like this
function foo(){ function bar(a){ i = 3; console.log( a + i ); } var i i = 0 for( i ; i <= 10; i++){ bar( i * 2 ) } } foo()
The function is promoted before the variable is promoted. After entering the for loop, bar is The parameter of the function is 0, so the first output is printed as 3. The focus is here i = 3. It will look for the declaration of i in the current scope. If there is no declaration, it will search up along the scope chain, in foo Found it, so i in foo becomes 3. After executing i, we get i= 4, and when we execute bar, we get the following 11. This keeps looping
2 , let and const
are two new ways to declare variables in ES6. Repeated declaration of the same variable in the same scope is not allowed. There is no variable promotion.
Since there is no variable promotion, this variable cannot be used before the variable is declared. Variables declared by var will be promoted. , but the value is undefined
In ES5, only functions have block-level scope, but in ES6, there is not only function scope. This is thanks to let and const
var b = 1; { let b = 2; } console.log(b); // 1
The curly braces are equivalent to a block-level scope
var a = [] for(let i=0; i<10;i++){ a[i] = function(){ console.log(i); }; console.log(i) //[function,function..] 总共10个 } console.log(a) a[8]() // 8
After changing let here to var, you will find that all output will become 10.
The i declared by var is actually a global variable, which is at the same level as the array a. Every time the for loop changes the value of i, it will be copied based on the original value. When the loop ends, i = 10, so no matter how it is called, the output is 10
The i declared by let is only in the current This block is valid. There is a block for each cycle, so a new variable i is created every time it loops. One difference between cont and let is that const is used to declare constants. Once declared, its value cannot be changed. But if const declares a reference type value
const a = {} a.name = 'circle' console.log(a); // Object {name: "circle"}
const stores the address value of this object, so it can only guarantee that the address value remains unchanged, but cannot guarantee the attributes in this object. cannot be changed
The above is the detailed content of What are var, let, const in JavaScript? Usage of var, let, const (code). For more information, please follow other related articles on the PHP Chinese website!