The statement

There are six declarative statements in JavaScript, all used to declare one or more identifiers. Identifiers here include variables, constants, and so on.

  • Let x… Declare the variable x. Cannot be read before assignment.
  • Const x… Declare constant x. Do not write.
  • Var x… Declare the variable x. Undefined can be read before assignment.
  • The function of x… Declare the variable x. This variable refers to a function.
  • The class x… Declare the variable x. This variable points to a class whose scope internally handles strict schemas.
  • The import… Import identifiers as constants (there are multiple modes and methods for declaring identifiers).

In addition to these six statements, there are two statements that have the potential to declare identifiers, although they are not strictly declarative statements (declarations are just their syntactic effects). These two statements refer to:

  • For (var | let | const x…). … The for statement has a variety of syntax for declaring one or more identifiers that can be used as loop variables.
  • The try… The catch (x)… The catch clause can declare one or more identifiers that can be used as exception object variables.
  • Both mean that JavaScript will be able to discover those declared identifiers through “static” parsing;
  • The variable/constant corresponding to the identifier “must” have been created in scope before the user code was executed.

From reading values to assigning values

The identifier convention for var declarations is called “varDelcs” and the “let/const” identifier is called “lexicalDecls”. JavaScript environments initially bind a undefined value to a “varName in varDecls” after it is created, whereas “lexicalNames” do not. So they are “no binding value yet” identifiers by default.

  • Functions are declared according to varDecls rules;
  • The interior of a class is in strict mode, so its name is treated as let;
  • Import Import names are handled as const

So, there are essentially only three processing modes for all declarations: var variable declarations, let variable declarations, and const constant declarations

The assignment

In JavaScript, the strict syntax for assignment is:

LeftHandSideExpression < = | AssignmentOperator > AssignmentExpression

So in JavaScript, the left and right sides of an assignment expression are “both” expressions!

The engine’s handling of variable declarations is divided into two parts: one is static, identity-based lexical analysis and management, which is always created as a name when the corresponding context is built; The other part is the expression execution process, which is the assignment of the above names. This process is also called binding.