Sitemap
Press enter or click to view image in full size

Scopes in JavaScript

--

In the last blog, we looked at Hoisting — how JavaScript moves declarations before execution.
Now, let’s talk about another core concept: Scope.

What is scope?

Scope is all about where you variables and function are accessible in your code

Types of Scope in JavaScript

1. Global Scope

A variable declared outside any function or block lives in the global scope.it can be accessed from anywhere in you code

2. Function Scope

Variables declared inside a function are only accessible within that function.

Get vignesh’s stories in your inbox

Join Medium for free to get updates from this writer.

3. Block Scope

let and const have block scope — meaning they exist only inside the { } where they are defined.

Press enter or click to view image in full size
Note: var does not have block scope — it ignores { } and acts as if declared in the function/global scope.

4. Lexical Scope (Closures)

Functions can access variables from the scope they were created in — even if you call them somewhere else.

Press enter or click to view image in full size

Why Scope Matters

  • Prevents naming conflicts.
  • Keeps your code organized.
  • Helps manage memory by controlling variable lifetime.

In the next blog, we’ll dive into a new concept — and trust me, it’s going to be an interesting one!

--

--