LearnThatStack Ace your next interview
JavaScript · question
Question 11 of 126

What is scope in JavaScript?

beginner
← All JavaScript questions
Re-explain

Scope decides where in the code a variable or function can be accessed. JavaScript has three main levels.

A variable in the global scope, declared outside any function or block, is accessible everywhere. A var or a parameter has function scope, accessible anywhere in its function. A let or const has block scope, accessible only inside the nearest braces. So a var inside an if block still belongs to the whole function, while a let there cannot be read once the block ends.

Scopes nest, so JavaScript resolves a name by checking the current scope first and then each outer scope. That lookup is the scope chain. Keep globals few, because every script on the page shares them and their names can clash.

var globalVar = "global";
function example() {
  var functionScoped = "function";
  if (true) {
    let blockScoped = "block";
    const alsoBlock = "block";
  }
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. Scope decides where in your code a variable or function can be accessed.
  2. Anything declared outside every function and block is in the global scope, so any code can reach it.
  3. A var or a function parameter has function scope, so it's visible anywhere in that function, while let and const are scoped to the nearest pair of braces.
  4. So a var declared inside an if block still belongs to the whole function, but a let declared there can't be read once the block ends.
  5. Scopes nest, so when JavaScript meets a name it checks the current scope first, then each outer scope in turn, and that lookup is the scope chain.
  6. I'd keep globals to a minimum, because every script on the page shares them and their names can clash.

Weak answers to avoid

  • Says var is block-scoped like let var ignores blocks and belongs to the whole function. Say let and const are block-scoped and var is function-scoped, then show it with a var inside an if.
  • Lists the levels but skips the scope chain Without the lookup order, current scope first and then each outer one, you can't explain shadowing or closures, and those are often the next question.
  • Mixes up scope with this Scope is about which variables code can see, fixed by where it's written. this depends on how a function is called. Keep the two apart.
  • Recites definitions with no example A two-line var versus let example inside a block shows you know the difference. Without one, the answer sounds memorized.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime