Skip to main content

Variables and constants

In JavaScript, you use variables and constants to store data.

Variables (let)

A variable is a container for storing a value, and that value can be changed later.

// Declaring and initializing a variable
let message = "Hello, World!";
console.log(message); // Hello, World!

// Changing the variable's value
message = "Hi, World!";
console.log(message); // Hi, World!
Beginner-friendly explanation

Using the let keyword lets you create a variable whose value you can change later. You can name a variable freely, but it's important to give it a clear name.

Constants (const)

A constant is a container that, once defined, cannot be replaced with another value.

// Declaring and initializing a constant
const PI = 3.14159;
console.log(PI); // 3.14159

// Trying to change a constant's value causes an error
// PI = 3.14; // TypeError: Assignment to constant variable.
Beginner-friendly explanation

A variable declared with const cannot be replaced with another value later. Use it for values that don't change (constants). By convention, the names of constant configuration values that never change are often written in uppercase.

const only means "cannot be reassigned," not "immutable"

What const forbids is only "reassigning the variable itself." The contents of an object or array can still be changed.

const user = { name: "Tanaka" };
user.name = "Sato"; // ✅ Changing a property is OK
console.log(user.name); // Sato

// user = {}; // ❌ Reassignment is an error (TypeError)

const numbers = [1, 2, 3];
numbers.push(4); // ✅ Adding to the array is also OK
console.log(numbers); // [1, 2, 3, 4]
Beginner-friendly explanation

It's easy to understand if you picture it as "you can't swap the box for another box, but you can swap out the contents of the box." If you don't want the contents changed either, one option is to use Object.freeze().

var (the old variable declaration)

You may see the var keyword in older code, but in modern JavaScript using let and const is recommended.

// Declaring a variable with var (not recommended)
var oldVariable = "The old way of writing";

// Problem: redeclaration is allowed
var oldVariable = "Accidentally redeclared"; // No error

The problem with var: function scope

Whereas let / const have their valid range (scope) per block {}, var ignores blocks and leaks out to the entire function.

// var ignores the block {} and is visible from the outside too
if (true) {
var leaked = "Visible outside the block too";
}
console.log(leaked); // Visible outside the block too (it leaked)

// let is block-scoped. It is not visible from outside the block
if (true) {
let blockScoped = "Only inside the block";
}
// console.log(blockScoped); // ❌ ReferenceError
Hoisting and the TDZ

With var, the declaration is hoisted to the top of the function, and accessing it before the declaration gives undefined. With let / const, on the other hand, accessing them before the declaration causes a ReferenceError due to the TDZ (Temporal Dead Zone).

console.log(a); // undefined (var does not error)
var a = 1;

console.log(b); // ❌ ReferenceError (let errors due to the TDZ)
let b = 2;

With let / const, access before the declaration errors immediately, so you can catch bugs earlier.

Summary

DeclarationReassignmentScopeAccess before declaration
const❌ Not allowedBlockTDZ (ReferenceError)
let✅ AllowedBlockTDZ (ReferenceError)
var✅ AllowedFunctionundefined (not recommended)

As a rule of thumb, it's enough to remember: use const by default, use let only when you need to reassign, and don't use var.

  • Data types — What kinds of "values" can you put in a variable?