Home > Article > Web Front-end > Comparison of let and const usage in ES6
are used to declare variables, but the declared variables are only where the let
command is located Valid within a code block
<span style="font-size: 18px"><code> {<br/> let a=12<br/> alert(a)//12<br/> }<br/> alert(a)//报错 找不到</code></span>
let does not cause variable promotion like var, so it must be declared first and then used.
console.log(foo); //undefined console.log(bar); //报错 var foo = 2; let bar = 3;
let does not allow repeated declaration of the same variable in the same scope.
## let a=12;
let a=5;//Error report
console.log(a)
<br/>
#constYou can also create block-scope variables, also only in the block-level scope where they are declared. Medium and effective. But its value is fixed, cannot be changed, and is read-only.
## }
alert(a)//Error report
Once a variable is declared, it must be initialized immediately and cannot be left until Assign value later.
//只声明不赋值就会报错 const foo; //报错
<br/>
The above is the detailed content of Comparison of let and const usage in ES6. For more information, please follow other related articles on the PHP Chinese website!