Facebook Pixel

Why does let throw but var returns undefined before the declaration line?

var is initialized to undefined during the memory phase, so it is usable early. let and const are not initialized until the declaration line, so the gap (TDZ) throws an error. This forces declare-before-use and catches bugs.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in The Temporal Dead Zone (TDZ): A Complete Explanation

The period between entering a scope where a let or const variable is declared and reaching the declaration line. Accessing the variable during this period throws ReferenceError: Cannot access 'x' before initialization.

No. typeof on a let or const variable in the TDZ throws ReferenceError. This is different from typeof on an undeclared variable, which returns 'undefined' without throwing.

Yes. Default parameters are evaluated left to right. A parameter that references a later parameter throws ReferenceError because that later parameter is still in the TDZ.

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
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.