Home > Web Front-end > Front-end Q&A > What does let do in JavaScript?

What does let do in JavaScript?

青灯夜游
Release: 2021-11-08 14:48:11
Original
3623 people have browsed it

In JavaScript, the function of let is to declare a variable, statement or expression at the block level scope, and optionally initialize it to a value; the syntax is "let variable name;" or "let variable name =value;".

What does let do in JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript let keyword

ES2015 (ES6) adds two important JavaScript keywords: let and const.

Use let to declare variables in block-level scope. The format of declaration is the same as the format of var to declare variables. There are three ways, as follows:

方式一:let 变量名;
方式二:let 变量名1,变量名2,…,变量名n;
方式三:let 变量名1=值1,变量名2=值2,…,变量名n=值n;
Copy after login

1) Use let to declare variables at once A variable can also be declared with multiple variables at one time. Use commas to separate different variables. For example:

let name; //一次声明一个变量
let name,age,gender; //一次声明多个变量
Copy after login

2) When declaring a variable, you do not need to initialize it (i.e. assign an initial value), in which case its value defaults to undefined; you can also initialize the variable while declaring it. For example:

let name = "张三"; //声明的同时初始化变量
let name = "张三",age = 20,gender; //在一条声明中初始化部分变量
let name = "张三",age=20,gender = ’女’; //在一条声明中初始化全部变量
Copy after login

3) The specific data type of the variable is determined according to the data type of the assigned value, for example:

let message = "hello";//值为字符串类型,所以message变量的类型为字符串类型
let message = 123; //值为数字类型,所以message变量的类型为数字类型
let message = true;//值为布尔类型,所以message变量的类型为布尔类型
Copy after login

The difference between let and var is that the variable it declares can only be Globally or for 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
}
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What does let do in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template