JavaScript Hoisting
Hoisting is JavaScript’s default behavior of moving declarations to the top.
JavaScript Declarations are Hoisted
In JavaScript, a variable can be declared after it has been used.
In other words; a variable can be used before it has been declared.
Example 1 gives the same result as Example 2:
Example 1
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
var x; // Declare x
Example 2
var x; // Declare x
x = 5; // Assign 5 to x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x; // Display x in the element
To understand this, you have to understand the term “hoisting”.
Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
The let and const Keywords
Variables defined with let
and const
are hoisted to the top of the block, but not initialized.
Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared.
Using a let
variable before it is declared will result in a ReferenceError
.
The variable is in a “temporal dead zone” from the start of the block until it is declared:
Example
This will result in a ReferenceError
:
carName = "Volvo";
let carName;
Using a const
variable before it is declared, is a syntax errror, so the code will simply not run.
Example
This code will not run.
carName = "Volvo";
const carName;
JavaScript Initializations are Not Hoisted
JavaScript only hoists declarations, not initializations.
Example 1 does not give the same result as Example 2:
Example 1
var x = 5; // Initialize x
var y = 7; // Initialize y
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x + " " + y; // Display x and y
Example 2
var x = 5; // Initialize x
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x + " " + y; // Display x and y
var y = 7; // Initialize y
Does it make sense that y is undefined in the last example?
This is because only the declaration (var y), not the initialization (=7) is hoisted to the top.
Because of hoisting, y has been declared before it is used, but because initializations are not hoisted, the value of y is undefined.
Example 2 is the same as writing:
Example
var x = 5; // Initialize x
var y; // Declare y
elem = document.getElementById("demo"); // Find an element
elem.innerHTML = x + " " + y; // Display x and y
y = 7; // Assign 7 to y
Declare Your Variables At the Top !
Hoisting is (to many developers) an unknown or overlooked behavior of JavaScript.
If a developer doesn’t understand hoisting, programs may contain bugs (errors).
To avoid bugs, always declare all variables at the beginning of every scope.
Since this is how JavaScript interprets the code, it is always a good rule.
39 Responses
buy levaquin 250mg sale order levofloxacin 250mg pills
dutasteride generic zofran price ondansetron 8mg uk
spironolactone pill purchase aldactone without prescription fluconazole 100mg price
order ampicillin 250mg pills buy ampicillin pills order erythromycin 250mg online
order sildenafil 100mg online robaxin buy online purchase methocarbamol online cheap
cheap sildenafil 50mg buy suhagra 100mg without prescription estrace 2mg ca
lamictal 200mg without prescription purchase mebendazole without prescription tretinoin price
order tadalis 10mg sale order tadacip 20mg for sale diclofenac price
isotretinoin 20mg usa amoxicillin pills cost zithromax 250mg
order indocin 75mg generic buy lamisil generic generic amoxicillin 250mg
buy tadalafil 5mg pill cialis overnight oral sildenafil 50mg
order arimidex without prescription generic viagra is it safe sildenafil 100mg uk
order deltasone 10mg online where to buy cialis generic sildenafil 50mg cost
buy isotretinoin sale stromectol 3mg usa oral stromectol cost
order provigil 200mg pill modafinil 100mg usa acetazolamide medication
buy generic doxycycline 100mg vardenafil 20mg canada lasix 100mg without prescription
altace sale order azelastine 10ml for sale azelastine price
order catapres 0.1 mg pill spiriva 9 mcg us order tiotropium bromide pill
buspar tablet buspirone 10mg brand generic oxybutynin 5mg
hytrin brand order sulfasalazine 500 mg generic azulfidine usa
alendronate 70mg cost famotidine 40mg brand order famotidine 40mg for sale
buy olmesartan 10mg online verapamil 240mg oral diamox online buy
order tacrolimus 5mg without prescription buy requip 1mg generic urso without prescription
buy isosorbide 40mg isosorbide 40mg pills telmisartan medication
bupropion without prescription strattera generic seroquel uk
order molnunat 200 mg pill purchase naproxen generic order lansoprazole 30mg online
sertraline 50mg uk Buy cheap viagra online brand viagra
buy imuran 100 mcg online cheap pantoprazole online buy buy generic viagra 50mg
generic tadalafil 20mg buy cialis 40mg for sale viagra australia
cialis for daily use order symmetrel 100mg pills purchase symmetrel online
naltrexone buy online albenza online order buy abilify 30mg generic
order avlosulfon 100mg without prescription buy generic dapsone 100mg purchase perindopril online cheap
buy medroxyprogesterone 10mg generic purchase biltricide generic periactin 4 mg canada
modafinil 200mg oral stromectol oral ivermectina online
luvox 50mg oral luvox for sale online order glucotrol 5mg online cheap
order nootropil 800mg pills viagra 50mg sale order viagra 100mg online
zithromax 250mg for sale gabapentin 800mg pills order gabapentin 800mg sale
tadalafil 20mg without prescription cheap sildenafil 100mg buy viagra 100mg pills
lasix 100mg brand doxycycline sale hydroxychloroquine 200mg over the counter