JavaScript Scope

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Scope controls where a variable name can be used. let and const are block-scoped. var is function-scoped. Nested scopes can read outer bindings.

JavaScript Syntax

JavaScript
const outer = "o";
{
  const inner = "i";
  console.log(outer, inner);
}
Try it Editor

How It Works

  1. Outer. Visible inside the block.
  2. Inner. Only inside the braces.

Examples

Example 1 — Function scope

var is function-scoped.

JavaScript
function f() {
  if (true) var x = 1;
  console.log(x);
}
f();
Try it Editor

How It Works

  1. var ignores the if block boundary.

Example 2 — Block let

let stays in the block.

JavaScript
{
  let x = 1;
}
console.log(typeof x);
Try it Editor

How It Works

  1. x is not visible outside.

Example 3 — Shadowing

Inner name hides outer.

JavaScript
const n = 1;
{
  const n = 2;
  console.log(n);
}
console.log(n);
Try it Editor

How It Works

  1. Inner n shadows outer n.

Frequently Asked Questions

The region of code where a binding can be referenced.
See hoisting and temporal dead zone.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful