Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Variables in JavaScript: var, let and const

Understand JavaScript’s three declaration keywords: how their scope and initialization differ, when reassignment is allowed, and why const objects can still change.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript has three variable declaration keywords: var, let and const. In new code, use const when you will keep a name bound to the same value, and let when you need to reassign it. Learn var so you can understand older code and avoid its different scope and initialization behavior.

How var, let and const differ

The practical differences are scope, what happens if you access a binding before its declaration is reached, whether you can reassign it, and whether you can declare the same name again in the same scope.

As an Amazon Associate I earn from qualifying purchases.

Keyword Scope Access before initialization Reassignment Redeclaration in the same scope
var Function scope, or global scope outside a function Returns undefined before the initializer runs Allowed Allowed
let Block scope Throws ReferenceError in the temporal dead zone Allowed Not allowed; it is a syntax error
const Block scope Throws ReferenceError in the temporal dead zone Not allowed Not allowed; it is a syntax error

What scope means for each keyword

var ignores block boundaries

A var declared inside a function belongs to that function, not just to the nearest pair of braces. If declared outside a function, it has global scope. For example, after this block runs, count is still accessible:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
if (true) {
  var count = 1;
}
console.log(count); // 1

let and const stay inside the block

A block is code enclosed in braces, such as an if body or loop body. A let or const declared there is available only within that block:

if (true) {
  let message = "inside";
  const limit = 3;
}
// message and limit are not available here

What happens before a declaration is reached

var is declared before execution, but its assignment is not

A var binding is processed before code in its scope executes. Its initializer still runs at the line where it appears, so reading the name earlier produces undefined, not the later assigned value:

console.log(score); // undefined
var score = 10;

let and const have a temporal dead zone

A lexical binding declared with let or const exists in its scope before the declaration line, but code cannot access it until execution reaches and initializes that declaration. The intervening period is called the temporal dead zone (TDZ). Access during it throws a ReferenceError:

console.log(score); // ReferenceError
let score = 10;

Even typeof throws if the name is a lexical binding currently in the TDZ. MDN defines the TDZ as the interval “from the start of the block until code execution reaches the place where the variable is declared and initialized.” See MDN’s let reference. Whether to describe let and const as “hoisted” is debated; the TDZ is the behavior to understand.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reassignment, redeclaration and const objects

Use let when a binding needs a new value

let allows reassignment after declaration, but you cannot declare that same name again in the same scope:

let status = "pending";
status = "complete"; // allowed
// let status = "failed"; // SyntaxError in the same scope

const prevents rebinding, not mutation

A const declaration must include an initializer, and the identifier cannot later be assigned a different value. If it refers to an object or array, however, the object’s properties or contents can still change:

const settings = { theme: "light" };
settings.theme = "dark"; // allowed
// settings = {}; // TypeError: cannot reassign

The useful distinction is whether the binding changes, not whether the referenced data can change. Prefer const when the binding remains assigned to the same value; choose let when it must point to a different value.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Redeclaring names and global declarations

var permits duplicate declarations of the same name in the same scope. let and const do not: attempting to redeclare either in that scope is a syntax error. Reassigning an existing let is different from redeclaring it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In a classic script, a top-level var declaration creates a property on the global object. Top-level let and const do not. JavaScript modules instead use module scope, so “global” behavior depends on whether the code is a classic script or a module.

A practical choice for new code

  • Start with const when you do not need to assign a different value to the name.
  • Use let when the name needs reassignment, such as a counter that changes in a loop.
  • Use var when working with code that already relies on its function-scoped or redeclaration behavior; do not assume it behaves like let.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.