JavaScript Course
JavaScript
/
Beginner

Scope

Definition

Scope determines the accessibility (visibility) of variables, objects, and functions from different parts of the code. JavaScript has Global scope, Function scope, and Block scope.

Explain Like I'm New

Think of scope like rooms in a house with one-way glass doors. If you are in a small inner room (block/function scope), you can see out into the living room (global scope) and use things there. But someone in the living room cannot look into your small inner room and use your things.

Real World Example

If you define a variable `password` inside a `login()` function, the rest of the application (outside the function) has no idea that the `password` variable exists. It's safe inside its room.

Common Use Cases

  • •Preventing variable naming collisions
  • •Keeping internal states private
  • •Managing memory efficiently by discarding variables when the scope ends

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What is Global Scope?
  • What is the difference between Function Scope and Block Scope?

intermediate

  • What is Lexical Scope?
  • How do var, let, and const behave differently with respect to scope?

advanced

  • What is the Scope Chain?
  • How does scope relate to closures?

trick

  • Can a variable declared with var inside an if-statement be accessed outside of it?

Flash Cards

Question

What is the difference between Function and Block Scope?

Click to reveal answer
Answer

Function scope means a variable is only accessible within the function it was declared in. Block scope (introduced with let and const) means a variable is only accessible within the curly braces {} it was declared in, like inside an if-statement or loop.

Question

What is the Scope Chain?

Click to reveal answer
Answer

When JavaScript needs to find a variable, it looks in the current scope. If it doesn't find it, it goes one level up to the outer scope, and continues up until it reaches the Global scope. This path is the scope chain.

Question

Can a var inside an if-statement be accessed outside?

Click to reveal answer
Answer

Yes. 'var' is function-scoped, not block-scoped. So an if-statement block does not trap a 'var' declaration, allowing it to be accessed outside the block.