Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between let and var in es6

Shulou Source: shulou.com Published: 2022-05-31 21:56:53 09月22日 Update

This article mainly introduces "what is the difference between let and var in es6". In daily operation, I believe that many people have doubts about the difference between let and var in es6. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between let and var in es6?" Next, please follow the editor to study!

Differences: 1, let variables have block-level scope, while var variables have no block-level scope; 2, var variables have variable promotion (no temporary dead zone constraints), which can be used and then declared, while let variables do not have variable promotion (with temporary dead zone constraints) and must be declared before use; 3, let variables can not be repeatedly declared, while var variables can.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

ES6 added the let command to declare local variables. Its usage is similar to var, but the declared variables are valid only within the block of code in which the let command is located (block-level scope) and have temporary dead-zone constraints.

Let's first take a look at the interview questions that are promoted by var's common variables:

The title 1:var a = 99; / / the global variable af (); / f is a function, which is defined after the call, but the function declaration is raised to the top of the scope. Console.log (a); / / a = > 99, this is the global variable afunction f () {console.log (a); / / the current a variable is the following variable a declared to promote, the default value undefined var a = 10; console.log (a); / / a = > 10} / / output result: undefined1099

If the above topics are difficult to understand children's shoes, please systematically take a look at Lao Ma's free JS advanced video tutorial.

ES6 can use let to define block-level scope variables

Before ES6, we used var to declare variables, and JS only has functional scope and global scope, not block-level scope, so {} cannot limit the access scope of var declared variables.

For example:

{var I = 9;} console.log (I); / / 9

The new let of ES6 can declare variables with block-level scope.

{let I = 9; / / I variables are only valid in curly braces! } console.log (I); / / Uncaught ReferenceError: i is not defined

Unique Application of let combined with for cycle

Let is ideal for block-level scopes within for loops. The for loop body in JS is special, and each execution is a new independent block scope. After the variables declared by let are passed into the scope of the for loop body, they will not change and will not be affected by the outside world. Look at a common interview question:

For (var I = 0; I

Tags: Variable action loop command temporary dead zone code global function loop body topic learning outside local result output valid block common tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Shulou Tech Info OPPO Reno MariaDB