Using a let statement to declare the usage of scopes in JavaScript _ basics

Source: Internet
Author: User

Grammar

Let variable1 = value1

Parameters
Variable1
The name of the variable to declare.
Value1
The initial value assigned to the variable.

Note
use the LET statement to declare a variable whose scope is limited to the block in which it is declared. You can assign a value to a variable when you declare it, or you can assign a value to a variable in a script later.
Variables that use let declarations cannot be used before the declaration, or they will result in an error.
If you do not initialize your variable in a let statement, the JavaScript value undefined is automatically assigned to it.

Example:

var L = ten;
{Let
  L = 2;
  At this point, L = 2.
}
At this point, L = ten.

Additional ways to declare a is variable using let.
Let index;
Let name = "Thomas Jefferson";
Let answer =, counter, numpages = ten;
Let myarray = new Array ();

Block-level scopes

for (var i = 0; i < i++) {}
Console.log (i),//10 for

(Let j = 0; J < Ten; j) {}
Console.log (j);//"Refe RENCEERROR:J is not defined

There is no variable elevation

Console.log (a); Output undefined
console.log (b);//Error Referenceerror
Console.log (c);//Error Referenceerror
var a = 2;
Let B = 2;

Notice the difference between undefined and referenceerror.

Temporary Dead Zone (TDZ)
as long as you enter the current block-level scope, the variable you are using already exists, but before declaring it belongs to the dead zone and cannot be manipulated.
Note: typeof is no longer a 100% safe operation

typeof X; Referenceerror
typeof y//undefined let
x;

Do not allow duplicate declarations

Let x = 1;
Let X; "Syntaxerror:identifier ' x ' has already been declared

var y = 2;
var y = 3; y = 3

Block-level scopes

anonymous function notation
(function () {
 var tmp = ...;
 ...
} ());

Block-level scope notation
{let
 tmp = ...;
 ...
}

The strict mode of ES5 stipulates that functions can only be declared within the top-level scope and function, and other cases (such as if code blocks, circular blocks) will be declared with an error.

ES5
' use strict ';
if (true) {
 function f () {}//Error
}

ES6 because of the introduction of block-level scopes, this situation can be interpreted as a function declared within a block-level scope, so there is no error, but the curly braces that make up the block are not less

Error
' use strict ';
if (true)
 function f () {}

Declared global variable is no longer a window's property

"Use strict";
var a = 1;
Console.log (WINDOW.A)//1 let

b = 1;
Console.log (window.b)//undefined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.