Home JavaScript Difference Between var, let, and const

Difference Between var, let, and const in JavaScript

Beginner ⏱ 8 min read Updated: Aug 2026
  • var, let, and const are all used to declare variables in JavaScript
  • var is function-scoped; let and const are block-scoped
  • var and let can be reassigned; const cannot be reassigned
  • var can be redeclared; let and const cannot be redeclared in the same scope
  • let and const were introduced in ES6 to fix var's inconsistent behaviour
  • const is the recommended default for values that will not change

Why Compare var, let, and const?

JavaScript gives you three different keywords for declaring a variable, and each one follows its own rules around scope, reassignment, and redeclaration. Understanding exactly how they differ helps you choose the right one and avoid subtle bugs that are common when these keywords are mixed up carelessly.

Important: A good general rule is to use const by default, switch to let only when reassignment is needed, and avoid var in new code.

Side-by-Side Example

Example

<script>
var a = 1;
let b = 2;
const c = 3;
a = 10;   // works
b = 20;   // works
c = 30;   // throws an error
</script>
            
Result
Uncaught TypeError: Assignment to constant variable

Scope Comparison

The biggest difference between these keywords is scope. A var variable is visible anywhere inside the function it was declared in, even if it was created inside an if block or loop. A let or const variable, on the other hand, only exists inside the nearest pair of curly braces, which usually matches what beginners expect a variable's boundaries to be.

Comparison at a Glance

var

Function-scoped, reassignable, redeclarable, hoisted with undefined value.

let

Block-scoped, reassignable, cannot be redeclared in the same scope.

const

Block-scoped, cannot be reassigned, cannot be redeclared, must be initialised.

Best Practice

Use const by default, let when reassignment is needed, and avoid var.

Redeclaration Behaviour

Another key difference is whether a variable name can be declared again in the same scope. With var, redeclaring the same name causes no error at all, and the second declaration simply overwrites the first. With let and const, trying to declare the same variable name twice in the same block causes JavaScript to throw an error, which actually helps you catch naming mistakes early instead of silently overwriting data.

Hoisting Differences

All three keywords are hoisted, meaning JavaScript is aware of them before the line where they appear, but they behave differently when accessed early. A var variable accessed before its declaration line simply returns undefined, while accessing a let or const variable before its declaration throws an error, because both sit in what is called the temporal dead zone until their declaration line runs.

Which One Should You Use?

For most modern JavaScript projects, const should be your first choice for any value that will not be reassigned, such as configuration values or references to objects and arrays you plan to edit internally. Use let when you know a value will change, such as a loop counter or a running total. Reach for var only when you are maintaining older code that already relies on it, since let and const offer more predictable and safer behaviour overall.