let in javascript is a new keyword in ES6. let allows us to declare a variable, statement or expression that is scoped or restricted to the block level. Variables declared by let can only be global or the entire function block.
#The operating environment of this article: windows10 system, havascript 1.8.5, thinkpad t480 computer.
ES2015 (ES6) adds two important JavaScript keywords: let and const.
The let keyword allows you to declare a variable, statement, or expression that is scoped or restricted to the block level.
Different from var, the variables it declares can only be global or the entire function block. In other words, variables declared by block-level == { }
let are only available in the block or sub-block in which they are declared. This is similar to var. The main difference between the two is that the scope of the variable declared by var is the entire enclosing function, while the scope of the variable declared by let is the block.
function varTest() { var x = 1; if (true) { var x = 2; // 同样的变量! console.log(x); // 2 } console.log(x); // 2 } function letTest() { let x = 1; if (true) { let x = 2; // 不同的变量 console.log(x); // 2 } console.log(x); // 1 }
Related video tutorial sharing: javascript video tutorial
The above is the detailed content of What is let in javascript. For more information, please follow other related articles on the PHP Chinese website!