Variables: let & const
Store and name data with let and const — the foundation every program is built on.
Imagine calculating a shopping cart total. Without a way to remember values, you'd have to type the price of every item into every calculation. Change one price and you'd have to hunt down every copy.
A variable solves this: it's a named container for a value. You give a value a name once — then use the name everywhere else.
let itemPrice = 29.99;
let quantity = 3;
let total = itemPrice * quantity;
console.log(total); // 89.97Think of a variable as a labelled box: the label is the name (itemPrice), and the box holds the value (29.99). When JavaScript sees the name, it looks inside the box.
Creating a variable is called declaring it. Putting a value in is called assigning (that's what = does — it means "store this", not "equals"). Giving it a new value later is reassigning:
let score = 0; // declare score, assign 0
score = 10; // reassign — the box now holds 10
score = score + 5; // read the current value (10), add 5, store 15
console.log(score); // 15JavaScript has two modern keywords for declaring variables:
let score = 0; // let — the value CAN be reassigned later
const MAX_LIVES = 3; // const — locked; reassigning is an errorThe rule of thumb every professional follows:
- Default to
const - Switch to
letonly when you know the value will change
(There's a third, older keyword — var — that modern code avoids. You'll see why in the next lesson.)
What happens when you try to reassign a const?
const lives = 3;
lives = 5;
console.log(lives);- A3
- B5
- Cundefined
- DTypeError: Assignment to constant variable.
Variable names follow a few rules — and a few conventions:
Rules (JavaScript enforces these):- Use letters, digits,
_, and$— but a name can't start with a digit - No spaces:
user nameis invalid - Names are case-sensitive:
scoreandScoreare two different variables
- Use camelCase: first word lowercase, each new word capitalised —
itemPrice,userEmail,isLoggedIn - Make names descriptive:
remainingLivesbeatsx. Code is read far more often than it's written - Constants that never change are sometimes written in ALL_CAPS:
MAX_LIVES,SPEED_OF_LIGHT
let userAge = 25; // ✅ clear camelCase
let 2ndPlace = "Bob"; // ❌ can't start with a digit
let user name = "Ann"; // ❌ no spaces allowedYou're building a game. The player's name never changes once set, but their score updates every time they collect a coin. Which declarations are correct?
- Aconst playerName = "Alex"; const score = 0;
- Bconst playerName = "Alex"; let score = 0;
- Clet playerName = "Alex"; let score = 0;
- Dvar playerName = "Alex"; var score = 0;
A variable can hold any type of value — a number, a string (text), and more you'll meet soon. You can even ask JavaScript what type something is with typeof:
let answer = 42;
console.log(typeof answer); // "number"
let greeting = "hello";
console.log(typeof greeting); // "string"JavaScript is dynamically typed — a fancy way of saying a variable isn't locked to one type. The same let variable could hold a number now and a string later:
let data = 42; // a number
data = "hello"; // now a string — JavaScript allows thisThat flexibility is convenient, but it also means you are responsible for keeping track of what a variable holds. Descriptive names (userAge suggests a number, userName suggests text) are your best defence.
Declare a constant for the speed of light (299792458 m/s):
___ SPEED_OF_LIGHT = 299792458;Follow the reassignments. What prints?
let stock = 10;
stock = stock - 3;
stock = stock - 3;
console.log(stock);- A10
- B7
- C4
- DError
- A variable is a named container for a value — declare it once, use the name everywhere
=means assign ("store this"), not "equals"letdeclares a variable you can reassign;constdeclares one that's locked- Default to
const; useletonly when the value will change - Names use camelCase, can't start with a digit, and should describe what they hold
typeoftells you a value's type — and a variable can hold any type
Next: the third keyword, var — and why modern JavaScript retired it.