JavaScript Learning Notes-ES6 Learning (ii) Let and const

Source: Internet
Author: User

There are two new commands in ES6: Let and Const.

Let command:

let is used to declare a variable, similar to Var, but the declared variable is valid only in the code block, there is no promotion of the variable, there is a temporary dead zone.

1. Valid only in code blocks

Unlike the Var command, a let declared variable is only valid in a block of code, such as

{  = 1;
var b = 2;}
Console.log (b); Print 2;console.log (a); This would cause error.

2. There is no variable promotion

The so-called variable promotion, which is the same scope, is called before declaring the variable.

function Test () {
temp = "Test" console.log (temp); // Would pint test. var temp;}

However, a variable that is declared with let does not have a variable promotion, meaning that it must be used after declaring the variable:

function Test () {  = "Test"  console.log (temp);   // reference error. Let   temp;}

3. Temporary Dead Zone

The so-called temporary Dead Zone refers to the effect of declared variables that do not accept variables with the same name. For example:

var tmp = 123; if (true) {  //  referenceerrorlet  tmp;}

The existence of a temporary dead zone makes the TypeOf no longer a safe operation (because it is possible that a variable cannot be promoted because it causes a reference error)

4. In addition, let does not allow duplicate declarations at the same scope

At the same block-level scope, Var declares that the same variable is possible, but let is not allowed.

5. To get a block-scoped variable, you can use the Do command:

 Do {  = f ();   * t + 1;};

Const command

The const command is used to declare read-only constants, like let, where scopes are block-level scopes, there are no variable elevation, and there are temporary dead zones.

When a const command declares a constant, only the address that the variable name points to is guaranteed to be the same, that is, if a const-declared constant points to an object, it does not guarantee that the property values in the object will not be changed. To ensure that the object property values are not changed, use Object.freeze () to freeze the object.

JavaScript Learning Notes-ES6 Learning (ii) Let and const

Related Article

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.