Facebook Pixel

How do closures affect scope chain questions in interviews?

Closures keep the scope chain alive after the outer function returns. The inner function can still access the outer variables. This is why makeCounter returns a function that keeps incrementing.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Scope Chain Interview Questions in JavaScript

Identify where each function was written (lexical scope), walk the scope chain from the current scope up to global, account for hoisting and shadowing, and remember that the chain does not change based on where the function is called.

Because JavaScript is lexically scoped. The scope chain is determined by where the function was written, not where it is called. The caller's variables are not in the function's scope chain.

var shadowing plus hoisting. An inner var with the same name as an outer variable is hoisted with undefined, so a read before the assignment line sees undefined instead of the outer value.

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