DevPath · Learn to code ESPTEN

Variables and data types

Declaring variables: let and const

Variables

Your program needs memory: a place to remember the user's name, a product's price, or whether someone is logged in. That's what variables are for.

A variable is, at heart, a name that points to a value stored in memory. It lets you reuse data and give it a meaningful name, instead of repeating the same value all over the place.

let and const

let age = 25;        // can change later
const name = "Ana";  // constant: cannot be reassigned

Reassigning

let points = 0;
points = points + 10; // now it's 10

If you try to reassign a const, JavaScript throws an error.

⚠️ Classic trap: const doesn't mean "immutable value", it means you can't reassign the name. If you store an object or an array in a const, its contents can still change. What stays fixed is what the variable points to, not what's inside.

Variable names

Examples

Declaring and using variables

const name = "Ana";
let age = 25;
age = 26; // birthday 🎂
console.log(name, "is", age, "years old");
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
Primitive data types →