Code Reference

Variables: let and const

JavaScript · Reference cheat sheet

Variables: let and const

JavaScript · Reference cheat sheet


📋 Overview

Modern JavaScript declares variables with let and const. Avoid var in new code. const prevents reassignment of the binding; let allows reassignment. Both are block-scoped.

🔧 Core concepts

KeywordReassign?ScopeTypical use
constNoBlock \{\}Default choice
letYesBlock \{\}Values that change
varYesFunctionLegacy only

const objects/arrays can still have their contents mutated — the binding cannot point to a new object.

💡 Examples

Basic declarations:

const name = "Sam";
let score = 0;

score = score + 10;
// name = "Alex"; // TypeError
console.log(name, score);

Block scope:

let x = 1;
{
  let x = 2;
  console.log(x); // 2
}
console.log(x); // 1

const with objects:

const user = { id: 1, role: "guest" };
user.role = "admin"; // OK — mutating property
// user = {};       // TypeError — rebinding

const nums = [1, 2];
nums.push(3); // OK
console.log(user, nums);

Temporal dead zone (use before declare):

// console.log(a); // ReferenceError
let a = 5;
console.log(a);

⚠️ Pitfalls

  • Prefer const until you know a value must change.
  • const ≠ deeply immutable.
  • Redeclaring the same let/const in one scope is a SyntaxError.
  • Forgetting let/const creates an accidental global in non-strict sloppy mode.

On this page