preface

What is the difference between var, let and const? I think most people are comfortable with this question. Your answer would be in the following ways:

  • varThere is a variable promotion, andletandconstThere is no variable promotion
  • varThe declared variable is added to the window object, andletandconstDeclared variables do not
  • letandconstDeclared variables cannot be declared twice
  • letandconstDeclared variables have temporary dead zones
  • constThe underlying type of the declaration cannot be modified. A const reference type can only modify the properties of the reference type and cannot reassign to the variable (constAn address has been identified that cannot be modified.
  • letandconstBlock-level scope exists, while VAR does not
  • Let is redeclared every time in the for loop (because let has block-level scope)

If you feel like you’ve said it all, interviewers love to rip you off, so here are two questions to rip you off.

Why can’t let and const be declared repeatedly? Why do let and const have temporary dead zones?

That’s crazy. How the fuck would I know? All right, all right, all right, all right. Let’s take care of this right now.

Why can’t let and const be declared repeatedly?

In the ES6 specification there is a term called Global Enviroment Records, which contains two contents, one is the Object Enviroment Record(which is not the same as the Window Object), Another is the Reclarative Enviroment Record.

  1. Function declaration and usagevarDeclared variables are added to enterObject Enviroment RecordIn the.
  2. useletDeclaration and UseconstThe declared variable is appendedReclarative Enviroment RecordIn the. The following are the pairs from the ECMAscript specificationvar.let.constSome constraints on.

Let’s look at ECMAscript constraints on var,let, and const declarations:

In other words:

  • usevarWhen declared, the V8 engine only checksDeclarative Enviroment RecordIf yes, an error is reported; otherwise, the variable is appendedObject Enviroment RecordIn the.
  • useletandconstWhen declared, the engine checks at the same timeObject Enviroment RecordandDeclarative Enviroment RecordIf the variable exists, an error is reported, otherwise the variable will be appendedDeclarative Enviroment RecordIn the.

This explains why variables declared with var can be declared repeatedly, but variables declared with let and const cannot.

Why do let and const have temporary dead zones?

Let’s start by asking the question, is it true that the declarations of variables declared by lets and const are not improved? Let’s do a simple test.

As you can see, When we add a variable A to the Object Enviroment Record using Object.defineProperty (using Object.defineProperty adds a variable to the Object Enviroment Record, Window. a does not add variables to the Object Enviroment Record.

  • When we letlet aandObject.definePropertyNo errors are reported when simultaneously in a code block
  • And when we execute firstObject.definePropertyReoccupy afterletThe statementa“, the browser reported an error.

In fact, there is already a problem, I think you are smart must have thought of it. The let declaration has been improved, but the variable that uses the let declaration has not been initialized. It does not even have a value of undefined. We use Chrome to test further:

You can see that Chrome says you can’t access A until initialization. So, the declaration of variables declared with let and const is promoted (it may seem strange that many books will say that there is no promotion of let and const, but the word promotion itself is uncanonical), but there is no initialization, not even a value of undefined.

This is where temporary dead zones come in.

Okay, did you get what I said? Hopefully this article helped you understand the difference between var,let, and const.

Var,let,const var,let,const var,let,const var,let,const O ( ̄▽ ̄)ブ ブ!