Is shadowing a good practice in JavaScript?
Generally no, for readability. Shadowed names can confuse readers and cause bugs (especially with var hoisting). Use distinct names unless you have a specific reason to shadow.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Variable Shadowing in JavaScript Explained
When a variable in an inner scope has the same name as a variable in an outer scope. The inner one shadows (hides) the outer one. The engine finds the inner first and stops searching the scope chain. The outer is unaffected but hidden.
Because the inner var is hoisted to the top of the function with undefined, shadowing the outer immediately. A read before the assignment line sees undefined, not the outer value.
No. It throws SyntaxError: Identifier 'x' has already been declared. var ignores block boundaries, so it would leak and conflict. Shadowing a var with a let is fine, but not the reverse.
Still have questions?
Browse all our FAQs or reach out to our support team
