How to use let in javascript
This article mainly explains "how to use let in javascript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use let in javascript".
Let in javascript is a new keyword in ES6, and 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 entire function blocks.
This article operating environment: 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.
Unlike var, the variables it declares can only be global or entire function blocks. In other words, block level = {}
Variables declared by let are only available in the blocks or subblocks they declare, similar to var. The main difference between the two is that the scope of the variable declared by var is the entire closed function, while the scope of the declaration of let is block.
Function varTest () {var x = 1; if (true) {var x = 2; / / same variable! Console.log (x); / / 2} console.log (x); / / 2} function letTest () {let x = 1; if (true) {let x = 2; / / different variables console.log (x); / / 2} console.log (x) / / 1} Thank you for reading, the above is the content of "how to use let in javascript". After the study of this article, I believe you have a deeper understanding of how to use let in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!