Facebook Pixel

What is the difference between hoisting of var and let in JavaScript?

var is hoisted AND initialized to undefined, so it is usable early (returns undefined). let and const are hoisted (memory allocated) but NOT initialized, so they sit in the TDZ and throw ReferenceError if accessed early.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How let and const Are Hoisted in JavaScript

Yes, memory is allocated for them during the memory allocation phase. But unlike var, they are not initialized to undefined. They sit in the Temporal Dead Zone until the declaration line, and accessing them early throws ReferenceError.

Because an inner let with the same name as an outer variable shadows the outer from the start of the block. If it were not hoisted, the outer would be found. The fact that it throws proves the inner let is hoisted and in the TDZ.

Yes, but like let and const, they are hoisted without initialization. They sit in the TDZ until the declaration line. Accessing a class before its line throws ReferenceError.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0