What is the difference between Declaring and Initializing a variable in JavaScript?

JavascriptWeb DevelopmentFront End Technology

The following is stated about declaration and initialization of a variable in ECMAScript specification −

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

The above defines the difference:

  • All variables are initialized with the value undefined.
  • Variables declarations are initialized with undefined upon the initialization of their lexical environment.
  • This initialization does not work as an assignment.
raja
Published on 25-Jan-2018 15:20:14
Advertisements