Interview Q&A / JavaScript

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the scope chain and how does JavaScript look up a variable?

SHORT ANSWER

The scope chain is the list of scopes from the current one outward to global. When you use a name, the engine checks the innermost scope first and moves outward until it finds it, or throws a ReferenceError.

Explain it simply

Looking for your charger at home: you check your own bag first, then your room, then the living room, then ask the neighbour. You stop the moment you find one.

JavaScript does the same with variable names. Every function and block has its own scope, and each one points to the scope that wraps it. That linked list is the scope chain. A lookup walks it from inside to outside, and the first match wins.

This explains shadowing: if an inner scope has a variable with the same name, the outer one is hidden there. If the walk reaches global and finds nothing, you get a ReferenceError.

Example

js
const city = "Pune";

function outer() {
  const city = "Nagpur";      // shadows the global one
  function inner() {
    return city;              // found in outer, lookup stops
  }
  return inner();
}

console.log(outer());   // Output: Nagpur
console.log(city);      // Output: Pune

What to say in the interview

"Each scope holds a reference to its parent, forming a chain. Identifier lookup starts in the innermost scope and goes outward, and the first match wins, so inner variables shadow outer ones. Not finding the name anywhere gives a ReferenceError."

Common mistakes

  • Thinking the inner variable overwrites the outer one. It only hides it inside that scope.

#javascript#scope

Related questions

Practise more JavaScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open JavaScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.