Skip to content
Laviyx Learn

Try

JavaScript course menu

JavaScript Variables: let, const and var Explained

Learn JavaScript variables: declaring with let and const, when to use each, why var is avoided, reassignment versus mutation of const objects, naming rules, undefined, and declaring several variables.

Beginner5 min readUpdated
On this page

A variable is a named container for a value. You create one with a keyword, a name and (usually) a value.

let and const#

JavaScript
let score = 10;
const playerName = "Asha";

console.log(playerName, score);

score = 15;            // let variables can be reassigned
console.log(playerName, score);
Output
Asha 10
Asha 15
KeywordCan be reassigned?Use for
constNoValues that will not be replaced: most variables
letYesValues that change: counters, totals, loop variables
varYesOld code only (see below)

Reassigning a const is an error:

JavaScript
const pi = 3.14159;
try {
  pi = 3;
} catch (error) {
  console.log(error.name + ": " + error.message);
}
Output
TypeError: Assignment to constant variable.

(try and catch let the program continue after an error so we can print it; they are covered in Error Handling.)

Default to const. Use let only when you know the value must change. This makes code easier to follow: when you see const, you know the name always refers to the same value.

Declaring without a value#

A let variable declared without a value holds undefined until you assign one. A const must be given its value immediately:

JavaScript
let city;
console.log(city);
city = "Pune";
console.log(city);
Output
undefined
Pune

const does not freeze objects#

const stops the variable from being reassigned, but if it holds an object or array, the object itself can still be changed:

JavaScript
const cart = ["pen"];
cart.push("notebook");     // allowed: changing the array's contents
console.log(cart);

const user = { name: "Ravi" };
user.name = "Ravi Kumar";  // allowed: changing a property
console.log(user);
Output
[ 'pen', 'notebook' ]
{ name: 'Ravi Kumar' }

What is not allowed is cart = ["something else"], which would point the variable at a different array.

Why not var?#

var is the original way to declare variables, and you will see it in older code and tutorials. It has two behaviours that cause bugs:

  1. Function scope: a var inside an if or loop block is visible outside the block.
  2. Hoisting to undefined: you can use a var before its declaration line and silently get undefined instead of an error.
JavaScript
if (true) {
  var leaked = "visible outside the block";
  let contained = "only inside the block";
}
console.log(leaked);
console.log(typeof contained);
Output
visible outside the block
undefined

let and const are block-scoped: they exist only inside the { } where they are declared. Use them, not var. Scope is covered fully in Scope and Hoisting.

Naming variables#

  • Use camelCase: totalPrice, isLoggedIn.
  • Make names descriptive: daysUntilExam rather than d.
  • Name booleans as yes/no questions: isActive, hasPaid.
  • Names cannot start with a digit or be reserved words like class or new.

Several at once#

JavaScript
let x = 1, y = 2, z = 3;
console.log(x + y + z);
Output
6

Most style guides prefer one declaration per line, which is easier to read and change.

Practice#

Practice exercise

Store a product name in a constant, a price of 250 and a quantity of 2 in variables, print the total, then change the quantity to 5 and print the new total. Which variables should be const?

Show solution
JavaScript
const product = "Notebook";
const price = 250;
let quantity = 2;

console.log(product, "total:", price * quantity);

quantity = 5;
console.log(product, "total:", price * quantity);
Output
Notebook total: 500
Notebook total: 1250

Only quantity changes, so it is the only let.

Key takeaway#

Finished this lesson?