Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between let and var in es6

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report