
Variables
A variable is a binding between a name & a value. A variable is:
- ▪ declared when a
const
,let
orvar
keyword is used with a name:let myVar
- ▪ initialized when an initial value is assigned to it:
myVar = 1
- ▪ reassigned when its value changes:
myVar = 2
Although you can declare & initialize a variable on the same line of code: let myVar = 1
, the engine will declare & initialize it in 2 different steps.

Primitive vs. Object Binding
JavaScript has 7 types:
- ▪
null
- ▪
undefined
- ▪
boolean
- ▪
string
- ▪
number
- ▪
symbol
- ▪
object
All the types are primitives except for object
.
- ▪ When you assign a primitive to a variable,
let myVar = 1
, the value1
is bound to the namemyVar
. - ▪ When you assign an object to a variable,
let myVar = { a: 1 }
, the value isn't bound tomyVar
. Instead, an object reference is bound tomyVar
. The memory address of{ a: 1 }
.
When assigning or passing variables of a primitive, a copy of the value is created.
/index.js
Console
// primitive binding var var1 = 1 var var2 = var1 var var3 = var1 var2++ console.log(var3)
When assigning or passing variables of an object, a copy of the reference is created. If 2 variables are assigned the same object, changing the value of 1 will change the other.
/index.js
Console
// object binding var var1 = { a: 1 } var var2 = var1 var var3 = var1 var2.a++ console.log(var3)
When comparing objects using the equality opertor, ===
, the results can be unintuitive. See Referential Equality for details.

Redeclaring
Redeclaring a var
will result in a no-op (nothing will happen on any additional declarations).
Redeclaring a const
or let
results in an error.
/index.js
Console
let myVar; let myVar;

undefined vs. undeclared
undefined
is the default value of a variable or property.
/index.js
Console
let myVar; console.log(myVar);
Attempting to access a variable that hasn't been declared throws a reference error.
Although the error states myVar is not defined
, a more accurate description is myVar is undeclared
.
/index.js
Console
myVar
Be aware, the typeof
operator returns "undefined"
for an undeclared variable.
/index.js
Console
console.log(typeof myVar)