📜var
🚧 under construction
Stop using var❗ ( 👉see why❗)
( ⭐ var is a statement, not a declaration❗)
declares a variable in function scope / global scope ( and does a lot of side effects❗).
var
is a reserved word❗
is a statement, not a declaration❗( 👉 var is a statement❗️❗)
doesn't have block scope. ( 👉 var has no block scope❗️❗)
👉 compare: function declaration
in global scope,
var / function are implemented as global object property❗
these properties cannot be deleted with delete❗
Variable Statement
var statement declares variables that are scoped to the running execution context's VariableEnvironment.
var variables are created when their containing Environment Record is instantiated and are initialized to undefined when created.
Within the scope of any VariableEnvironment a common BindingIdentifier may appear in more than one VariableDeclaration but those declarations collectively define only one variable.
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.
var is a statement❗️(side effects)
global var / function is global object property❗️(side effect)
accessing var before declaration gets undefined❗️(unexpected result)
var has no block scope❗️ (unexpected result)
var in block can't shadow outer let❗️(unexpected result)
var can shadow parameter even in strict mode❗️(bad practice)
var redeclaration applied even in strict mode❗️(bad practice)
VariableEnvironment
Last updated
Was this helpful?