First, a common question to understand is, what exactly is the relationship between ECMAScript and JavaScript?

ECMAScript is an internationally approved standardized scripting language. JavaScript consists of ECMAScript, DOM and BOM. ECMAScript is the language specification of JavaScript, and JavaScript is the implementation and extension of ECMAScript. In 2011, ECMAScript 5.1 was released. In June 2015, ECMAScript 6 was adopted as an international standard.Copy the code

1. Block-level scope {}

ES5 scope: global scope, function scope. There is no concept of block scope.

Block-level scopes have been added in ES6. Block scopes are included by {}. The {} in the if and for statements are also block scopes. `

{ var a = 1; console.log(a); // 1 } console.log(a); // 1 // Variables defined by var can be accessed across block scopes. (function A() { var b = 2; console.log(b); / / 2}) (); // console.log(b); If (true) {var c = 3; if(true) {var c = 3; } console.log(c); // 3 for(var i = 0; i < 4; i ++) { var d = 5; }; console.log(i); // 4 (I is already 4 at the end of the loop, so I is 4 here) console.log(d); If and for statements are block scoped, not function scoped. If and for statements are block scoped.Copy the code

Var, let, const

  1. Variables defined by var have no concept of blocks and can be accessed across blocks rather than functions.
  2. Variables defined by lets can only be accessed in the block scope, not across blocks or functions.
  3. Const is used to define a constant, must be initialized (must be assigned), can only be accessed in the block scope, and cannot be modified pointing (can change the value of the object).
// block scope {var a = 1; let b = 2; const c = 3; // c = 4; Var aa; let bb; // const cc; / / an error console. The log (a); // 1 console.log(b); // 2 console.log(c); // 3 console.log(aa); // undefined console.log(bb); // undefined } console.log(a); // 1 // console.log(b); // console.log(c); Function A() {var d = 5; let e = 6; const f = 7; console.log(d); // 5 console.log(e); // 6 console.log(f); / / 7}) (); // console.log(d); // console.log(e); // console.log(f); / / error < / script >Copy the code

3. Whether object properties defined by const can be changed

This is a problem often encountered in the interview. It said that const could not be modified, so I readily said that it could not. But after the actual test, I found that it was wrong.

Const person = {name: 'long', sex: 'male'} person.name = 'test' console.log(person.name)Copy the code

What happens when you run the above code and find that the Name property of the Person object has indeed been modified?

Since objects are referential, only Pointers are held in Person, which means that const only guarantees that the pointer does not change. Modifying an object’s properties does not change the object’s pointer, so it is allowed. That is, a reference type defined by const is allowed to change no matter how long the pointer does not change.

Assignment to constant variable (person) Assignment to constant variable (person)

Const person = {name: 'long', person = {name: 'test', sex: 'male'}Copy the code

Const is just a pointer to a memory space. The contents of the memory space can be changed, but const pointing to a new space is an error.

Var and let are both variables. Var has no block-level scope, so it gets promoted. Let has block-level scope, so it doesn’t get promoted. You cannot change the name of the warehouse.