Can you shadow a const with a let in a block in JavaScript?
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.
Verify This Answer
Cross-check this information using these trusted sources:
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.
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.
Still have questions?
Browse all our FAQs or reach out to our support team
