Facebook Pixel

Why does an inner let shadow the outer from the start of the block in JavaScript?

Because the inner let is hoisted (memory allocated) at the start of the block, so it shadows the outer immediately. But it is in the TDZ until its declaration line, so accessing it early throws instead of falling back to the outer.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in TDZ Interview Questions for let and const in JavaScript

ReferenceError: Cannot access 'x' before initialization. The message is specific to the TDZ, different from 'x is not defined' which means the variable does not exist at all.

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

They are evaluated left to right. A parameter that references a later parameter throws ReferenceError because that later parameter is still in the TDZ. For example, function foo(a = b, b = 2) throws.

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