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


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.

Updated on: 13-Jun-2020

387 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements