New features of ES6-added keyword let, const

Source: Internet
Author: User

What is ECMAScript? First, we all know that JavaScript consists of three parts: Ecmascript,dom,bom;one of the ECMAScript is the syntax specification for JavaScript. ECMAScript defines a lot of things, such as:
    • Syntax-----parsing rules, keywords, statements, declarations, actions, etc.
    • Type-----Boolean, number, String, object, etc.
    • Prototypes and inheritance
    • Built-in objects, standard libraries of functions----------JSON, Math, array methods, object methods, etc.
Browser compatible:Currently, Google and Firefox browsers are most compatible with the new features of ES6. and IE9 has problems. For incompatible browsers, we can use only conversion tools such as Babel. We use Nodejs's package management tool NPM to install Babel. Introduce browser.min.js in front of our JS file.   (1) New let keyword in ES6before we declare a variable, we use the keyword Var, what is the difference between the new let and Var?  the disadvantage of Var one:var arr=[];For ( var i=0;i<10;i++) {arr[i]=function () {Console.log (i);     };} Arr[8] ();  Console.log (i); The result is ten after the same change, there is no problem:var arr=[];For ( let i=0;i<10;i++) {arr[i]=function () {Console.log (i);     };} Arr[8] ();  Console.log (i); The result is 8 this is because the original JS is not block-level scope, with VAR definition i, although defined in the For loop, but in the execution, will still be promoted. When the execution of Arr[8] (), the loop has been completed, then the i=10; so to execute arr[8] (), Console.log (i), i=10; instead, define i,i with let to exist only with the block-level scope of the For loop and not be promoted. (block, only the part of a group of {}.) ) the disadvantage of Var two:That is, variable elevation. var is defined by promotion; var a=1;(function () {alert (a); var a=2;})(); Undefined means only declaration, for assignment; after the same change let:var a=1;(function () {alert (a);Let a=2;})(); Error: Uncaught referenceerror:a is not defined (...) this is because, when defined with let, the definition of variable A is not promoted. And is closed within the block-level scope, and will not be affected by external a. When a (alert (a)) is used, a is not yet defined, so an error is encountered. Therefore, when using let, it is important to note that variables must be declared before they are used.  Note When using let:Note: Under the same block level scope, the same variable cannot be declared repeatedly. {var a=1;Let a=2;}//Error{Let a=1;Let a=2;}//Error Note two: Within a function, you cannot use let to re-declare the parameters of a function. function Say (word) {Let word= ' hello ';alert (word);}say (' Hi '); Error  (2) The new const keyword in ES6Const is the abbreviation of a constant (contsant), which is specifically used to declare a constant when compared to let. Features:
    • Cannot be modified
Const name= ' a ';name= ' B '; Error
    • Block-level scopes
if (1) {Const name= ' a ';     }alert (name); Error
    • There is no variable promotion, you must declare it before using
if (1) {alert (name); ErrorConst name= ' a ';     }  
    • Cannot declare the same variable repeatedly
var name= ' a ';Const name= ' B '; Error
    • You must assign a value after the declaration
const name; Error Note: When a constant is an object:const person={' name ': ' A '};person.name= ' B ';person.age= ' + ';Console.log (person)//{' name ': B, ' age ': ' 13 '}; The output is normal. this is because the const declares a constant of a reference type, using the address assignment. That is, the address is not modified, only the value in the address is modified.       ———————————————————————Reference Public Number: Web front-end Tutorials 

New features of ES6-added keyword let, const

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.