Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript var let and const

Difference Between var, let and const

The primary difference between var, let, and const lies in scoping mechanisms, reassignment rules, and how JavaScript handles them via hoisting.

ScopeRedeclareReassignHoistedNOTE
varNoYesYesYesUndefined
letYesNoYesYesNot initialized
constYesNoNoYesNot initialized

Variables declared with var are hoisted but initialized with undefined.

Variables declared with let and const are also hoisted, but not initialized.

Using a let or const variable before it is declared will result in a ReferenceError.

The time between the start of the block and the declaration is called the Temporal Dead Zone.

What is Not Good?

var does not have to be declared.

var is hoisted.

var binds to this.

Modern JavaScript standards recommend avoiding var entirely to minimize unintentional bugs.


Scope Rules

var is function-scoped:
If you declare a var variable inside a loop or an if block, it "leaks out" and is accessible anywhere outside the block.

let and const are block-scoped:
They are confined strictly to the nearest pair of curly braces {} (such as loops, functions, or conditional blocks).

As a rule of thumb, you should use const by default, and only use let when you have to reassign the variable after initialization.

You should avoid using var.

Featurevarletconst
ScopeFunction or global scopeBlock-scope { }Block-scope { }
ReassignmentCan be updatedCan be updatedCannot be updated
RedeclarationCan be redeclaredCannot be redeclaredCannot be redeclared
HoistingInitialized as undefinedHoisted, not initializedHoisted, not initialized

What is Good?

let and const have block scope.

let and const can not be redeclared.

let and const must be declared before use.

let and const does not bind to this.

let and const are not hoisted.

Example

if (true) {
  var firstName = "John";
  let lastName = "Doe";
}

let text1 = text2 = "unknown";

if (typeof firstName !== "undefined") text1 = firstName;
if (typeof lastName !== "undefined") text2 = lastName;
Try it Yourself »

Reassignment vs. Redeclaration

var allows you to accidentally re-declare the exact same variable name in the same scope, silently overwriting earlier values.

let can be updated/reassigned, but it throws an error if you attempt to re-declare it within the same scope.

const is strictly read-only. It requires an initial value immediately upon declaration and cannot be reassigned later.

Note on const objects:
You cannot reassign a const variable with a completely new value, you can still change the properties of an object or array declared with const.

Example

// Create user object
const user = { name: "Alice" };

// This will work
user.name = "Bob";
let text = user.name + " ";

// This will not work
try {
  user = { name: "Charlie" };
  text += user.name;
} catch(err) {
  text += err;
}
Try it Yourself »

Hoisting and the Temporal Dead Zone (TDZ)

All variables are hoisted to the top of their scope by the JavaScript engine, but they are treated differently:

var variables are hoisted and automatically assigned a default value of undefined. You can reference them before they are declared in the code without breaking the application.

let and const are hoisted but remain completely uninitialized. The region of code from the start of the block until the line they are declared is called the Temporal Dead Zone. Accessing them here instantly crashes with a ReferenceError.


Best Practice

Developers should follow these guidelines for modern JavaScript (ES6 - 2015+):

Always use const by default.
It prevents accidental reassignments and makes your code more predictable.

Only use let when:
You know its value needs to change later (inside a loop or a mathematical counter).

Never use var.
Its unpredictable scoping and the redeclaration rules are known to cause bugs.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->