Home > Web Front-end > JS Tutorial > body text

What is let in javascript

王林
Release: 2023-01-07 11:42:37
Original
17250 people have browsed it

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.

What is let in javascript

#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
}
Copy after login

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!

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