In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the role of let in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the role of let in JavaScript?"
In JavaScript, the role of let is to declare block-level scoped variables, statements, or expressions and optionally initialize them to a value; syntax "let variable name;" or "let variable name = value;"
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
JavaScript let keyword
ES2015 (ES6) adds two important JavaScript keywords: let and const.
Using let, you can declare variables that are scoped at the block level. There are three ways to declare variables in the same format as var, as shown below:
Mode 1: let variable name; Mode 2: let variable name 1, variable name 2, … Mode 3: let variable name 1 = value 1, variable name 2 = value 2,... , variable name n = value n
1) using let, you can declare one variable at a time, or you can declare multiple variables at a time, separated by commas. For example:
Let name; / / declare one variable at a time let name,age,gender; / / declare multiple variables at a time
2) you can declare a variable without initialization (that is, assigning an initial value). At this time, the default value is undefined;, or you can initialize the variable while declaring the variable. For example:
Let name = "Zhang San"; / / initialize variables let name = "Zhang San", age=20,gender; / / initialize some variables let name = "Zhang San", age=20,gender = 'female' in one declaration; / / initialize all variables in one declaration
3) the specific data type of the variable is determined according to the data type of the assigned value, for example:
Let message = "hello"; / / value is a string type, so the type of message variable is string type let message = 123; / / value is numeric, so the type of message variable is numeric type let message = true;// value is Boolean type, so the type of message variable is Boolean type
Let differs from var in that 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} at this point, I believe you have a deeper understanding of "what is the role of let in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.