Facebook Pixel

What is illegal shadowing in JavaScript?

You cannot shadow a let, const, or class with a var in the same scope. var is function-scoped and leaks out of blocks, so it would conflict with the outer let/const/class. The engine throws SyntaxError: Identifier has already been declared.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Illegal Shadowing in JavaScript Explained

Yes. Shadowing a var with a let is legal because let is block-scoped and does not leak out. There is no conflict. The reverse (shadowing a let with a var) is illegal.

Because var ignores block boundaries and would leak into the scope where the let is declared, creating two declarations of the same name in the same scope. The engine catches this at parse time.

Yes. Shadowing a const with a let (or another const) in a separate block or function scope is legal. The illegal case is only shadowing a let/const/class with a var in the same scope.

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.