JavaScript let / const / var

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

let and const are block-scoped. var is function-scoped and older. Prefer const by default, let when you must reassign, and avoid var in modern code.

JavaScript Syntax

JavaScript
const name = "Ada";
let score = 0;
score = 10;
console.log(name, score);
Try it Editor

How It Works

  1. const cannot rebind. name stays "Ada".
  2. let can reassign. score changes to 10.

Examples

Example 1 — Block scope

let/const live inside { }.

JavaScript
{
  let x = 1;
}
try { console.log(x); } catch (e) { console.log(e.name); }
Try it Editor

How It Works

  1. x is not visible outside the block.
  2. Access throws ReferenceError.

Example 2 — const object can mutate

const stops rebinding, not property changes.

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

How It Works

  1. user still points to the same object.
  2. Property n can change.

Example 3 — var is function-scoped

Why modern code avoids var.

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

How It Works

  1. var ignores the if block boundary.
  2. v leaks to the whole function.

Frequently Asked Questions

Use const when the binding will not be reassigned. Use let when it will.
You cannot use a let/const before its declaration line in that scope.

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