Facebook Pixel
Illegal Shadowing in JavaScript Explained

Can you shadow a var with a let in the same scope in JavaScript?

The answer

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.

More FAQs in Illegal Shadowing in JavaScript Explained

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.

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?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

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