- Ecmascript is the latest standard of the current JavaScript language specification, which is generally referred to as es6. However, because the standard specification was released in June 2015, it is also called ecmascript 2015.
- Let variable Declaration
Es2015 adds a variable declaration method:let, Similarvar, ThroughletThe declared variables are onlyletThe code block in which the command resides is valid.
Block-level scopes. Block-level codes automatically form a separate scope.
Why block-level scope?
- The inner variable may overwrite the outer variable.
- The cyclic variables used for counting are leaked as global variables.
UseletNOTE: If there is no variable escalation, you must declare the variable before using it. Otherwise, an error is returned. Duplicate declarations of variables with the same name are not allowed within the same scope; otherwise, an error is reported. Block-level scope. The outer code block is not affected by the inner code block. Variables in the inner scope cannot be read from the outer scope. The inner scope can define variables with the same name as the outer scope.
Use Cases: Let variables are generally used to declare the block.
Const variable Declaration
Const is also used to declare variables, but declared as constants. Once declaredValueIt cannot be changed. The constant value cannot be changed; otherwise, an error is returned.constIt is also a block-level scope. It is only valid within the block-level scope where the declaration is located. We do not recommend that you use the const variable in the block to promote it. You cannot declare it again.
Const application scenarios:
All variables that do not want the user to change are passedconstDeclared as a constantjavascript const fs = require(‘fs‘); const os = require(‘os‘);
PassconstThe declared constant cannot be changed by the constant address. If the constant is an object, it can still be modified.
javascript ‘use strict‘; const obj = { foo: ‘bar‘ }; console.log(obj.foo); // => bar obj.foo = ‘baz‘; console.log(obj.foo); // => baz
Knowledge about ecmascript6