Let keyword and const keyword

Source: Internet
Author: User

In es6, The let keyword and the const keyword are provided.

The declaration method of let is the same as that of VaR. Instead of VaR, let can be used to declare the variable, so that the variable can be restricted to the current code block.

When const is used to declare a constant, its value cannot be changed once it is set.

 

Let allows you to declare a variable, statement, or expression that is limited to a block-level scope. Unlike the VaR keyword, the declared variables can only be global or the entire function block.

Let Syntax:

let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];

The variables declared by let are only available in the declared block or sub-block. This is similar to var. The major difference between the two lies in that the scope of the variable declared by VAR is the entire closed function.

Let and VAR code example:

Function vartest () {var x = 1; if (true) {var x = 2; // the same variable! Console. log (x); // 2} console. log (x); // 2} function lettest () {Let X = 1; if (true) {Let X = 2; // different variable console. log (x); // 2} console. log (x); // 1}


Variables used when the function is not declared:

function func(){  undefined_var=110}

Before func () is called for the first time, the undefined_var variable does not exist, that is, undefined. After func () is called, undefined_var becomes a global variable.

 

Let keyword and const keyword

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.