JavaScript var vs let vs const: Simple Guide for Beginners
What is the difference between var, let and const in JavaScript? Learn which one to use, with tiny runnable examples and plain-English explanations.
What is the difference between var, let and const in JavaScript? Learn which one to use, with tiny runnable examples and plain-English explanations.
What is the difference between var, let and const in JavaScript? In short: const is for values that will not be reassigned, let is for values that will change, and var is the old way that you should avoid in new code. This guide explains each one with small examples you can copy and run.
You do not need any experience. We define every term the first time we use it. If you want to learn what a variable is first, read our guide to JavaScript variables and data types (linked below), then come back here.
A variable is a named box that holds a value, such as a number or some text. A declaration is the line of code that creates the box. JavaScript gives you three keywords (special reserved words) to declare a variable: var, let and const.
var city = "Paris";
let age = 25;
const pi = 3.14;Line 1 creates a variable called city with the text "Paris" using var. Line 2 creates age with the number 25 using let. Line 3 creates pi with the number 3.14 using const. All three work, but they behave differently, as you will see next.
Use const when the variable should always point to the same value. Reassigning means giving the variable a new value with the = sign. JavaScript stops you from doing that with const.
const siteName = "Fepiq";
console.log(siteName);
siteName = "Other"; // Error!Line 1 stores the text. Line 2 prints it to the console (the place where JavaScript shows messages). Line 4 tries to change it, and JavaScript throws an error: Assignment to constant variable. This is helpful, because it protects you from changing something by accident.
Use let when you know the value will change later, for example a counter or a score.
let score = 0;
score = score + 10;
score = 20;
console.log(score); // 20Line 1 starts the score at 0. Line 2 adds 10 to it. Line 3 replaces it with 20. Line 4 prints 20. No error happens, because let allows reassigning.
var is how JavaScript declared variables before 2015. It still works, but it has confusing behavior that causes bugs. The two main problems are scope and redeclaring.
Scope means the area of your code where a variable can be used. A block is any code between curly braces { }, such as inside an if statement. let and const stay inside their block. var leaks out.
if (true) {
var a = "I am var";
let b = "I am let";
}
console.log(a); // "I am var"
console.log(b); // Error: b is not definedInside the if block we create a with var and b with let. Outside the block, a still works, which surprises many beginners. b does not exist anymore, so JavaScript gives an error. The let behavior is what most people expect.
var user = "Anna";
var user = "Ben"; // no error
let team = "Red";
let team = "Blue"; // Error!With var, the second declaration silently replaces the first, so you might overwrite a variable by mistake. With let, JavaScript tells you the name was already declared. Errors like this help you find mistakes early.
Not exactly. const stops you from reassigning the variable. But if the value is an array or an object, you can still change what is inside it.
const fruits = ["apple", "banana"];
fruits.push("orange"); // allowed
console.log(fruits); // ["apple", "banana", "orange"]
fruits = ["kiwi"]; // Error!Line 2 adds an item to the array. This is allowed, because the variable still points to the same array. Line 5 tries to point fruits at a brand-new array, which const does not allow.
| Feature | var | let | const |
|---|---|---|---|
| Can be reassigned | Yes | Yes | No |
| Can be redeclared in same scope | Yes | No | No |
| Scope | Function | Block | Block |
| Needs a value when declared | No | No | Yes |
| Recommended today | No | Yes, when value changes | Yes, by default |
Follow this simple rule that most developers use:
This habit makes your code easier to read, because anyone who sees const knows the value will not be reassigned.
var is function-scoped and can be redeclared, while let and const are block-scoped. let allows reassigning a value, and const does not. Modern code uses const and let and avoids var.
Use const by default. Switch to let only when you know the variable needs a new value later, such as a counter inside a loop.
var ignores block scope and allows accidental redeclaring, which causes hard-to-find bugs. let and const were added in 2015 to fix these problems.
Yes. You can add, remove or change items, because the variable still points to the same array. You just cannot assign a completely new array to that name.
Yes. All modern browsers and current versions of Node.js support let and const, so you can use them safely today.
New to variables? Start with the basics of variables and data types, then return to this guide.
Read: JavaScript Variables & Data TypesWant to build a website or web app with a team that explains things clearly? Talk to Fepiq.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.