Facebook Pixel

What happens when the engine cannot find a variable in the scope chain?

It throws ReferenceError: x is not defined. This means the variable does not exist in any accessible lexical environment, from the current scope up to the global scope.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in The Scope Chain in JavaScript: How Variable Resolution Works

The linked list of lexical environments that the engine walks to resolve a variable. It looks in the current environment first, then follows outer references up to the global scope. If not found, it throws ReferenceError.

At definition time. The outer references are determined by where functions are written (lexical scope), not where they are called. This is why a function called from a different scope does not gain access to that scope's variables.

A local variable with the same name as an outer one shadows the outer. The engine finds the local first and stops searching, so the outer variable is hidden but not affected.

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