Our study or work will involve JavaScript, so we must also understand JavaScript. The let statement is a large part of JavaScript. This article will help you understand and master the let statement, let's study it carefully.
Using the let statement, allows you to create block-scoped local variables in JavaScript. The let statement was introduced in the ECMAScript 6 standard for JavaScript. Before you dive into let statements, I recommend you check out the Ignite UI based on the Infragistics jQuery library, which can help you write and run web applications faster. You can use the Ignite UI of the JavaScript library to quickly solve complex LOB needs in HTML5, jQuery, Angular, React or ASP.NET MVC. (You can download a free trial of Ignite UI here.)
Prior to ECMAScript 6, JavaScript had three types of scope:
Global scope
Function scope
Vocabulary scope
To explore the let statement in detail, consider the following code snippet:
function foo() { var x = 9; if (x > 5) { var x = 7; console.log("Value of x in if statement = " + x); } console.log("Value of x outside if statement = " + x); } foo();
The output obtained by the above code:
In the above code, we declare the variable x using the var statement. Therefore, the scope of variable x is the scope of the function. The variable x inside the if statement is the variable x created outside the if statement. Therefore, when you modify the value of variable x within the if statement block, the values of all references to variable x in the function will also be modified.
To avoid this, you need to use block-level scope. The let statement allows you to create block-scoped local variables.
Modify the above code snippet and use the let statement to declare the variable:
function foo() { var x = 9; if (x > 5) { let x = 7; console.log("Value of x in if statement = " + x); } console.log("Value of x outside if statement = " + x); } foo();
In the above code snippet, we use the let statement to declare the scope-level local variable x. Therefore, updating the value of variable x inside the if statement does not affect the value of variable x outside the if statement.
The following is the output of the above code:
is different from variables declared using function scope (or global scope) , variables declared using let are block-scoped: they only exist within the block in which they are defined.
Variable promotion
The promotion of variables declared using let is different from variables declared using var. Therefore, variables declared using let do not have variable hoisting, which means variables declared using let are not moved to the top of the execution context.
To understand this better, take a look at the following piece of code:
function foo() { console.log(x); console.log(y); var x = 9; let y = 67; } foo();
As output, you will get a ReferenceError for the variable y, which is declared using the let statement. Variables declared using let are not hoisted above the execution context.
Redeclaring a variable
You cannot use let to redeclare a variable in the same function or block. Doing so will cause a syntax error. Please look at the following code:
function foo() { if(true){ let x = 9; let x = 89; } } foo();
A syntax error will appear when running the above code, as shown below:
Temporary dead zone
Sometimes, variables declared using let can cause temporary dead zones. In the following code, let x=x+67 will throw an x undefined exception.
The reason why this error occurs is because expression(x + 67) seeks the value of local variable x within the scope of the if block, not the value of local variable x within the scope of the function value. Run the above code and you will get an exception like this:
Block-level scoping is one of the most important features of any programming language and comes with With the introduction of the let statement in ECMAScript 6, JavaScript now also has this functionality. Using the let statement allows the creation of a variable scoped within the block scope. This can solve many problems, such as accidental modification of global scope variables, local variables in closures, and help write cleaner code.
In the next article in the Understanding JavaScript series, we will introduce the rest parameter in JavaScript functions. You can learn more about this on the php.cn website. Also, don’t forget to check out Ignite UI, which can be used with HTML5, Angular, React or ASP.NET MVC to help create rich internet applications.
The above is about understanding JavaScript and the full text of the let statement. Interested friends can discuss it together.
Related reading:
The above is the detailed content of Understand JavaScript and understand the let statement. For more information, please follow other related articles on the PHP Chinese website!