Section 2 · Variables & types

Boxes that hold values

A variable is a labelled box you put a value in, so you can use and change it later.

🎯 In one line: let makes a box you can change, const makes one you can't — both hold a value with a name.

1let and const

let score = 10;      // a box named score, holding 10
score = 15;          // fine — let can change

const name = "Aisha"; // a box that must NOT change
// name = "Bruno";    // ERROR — const can't be reassigned
Which do I use? Reach for const by default (it prevents accidental changes). Use let only when you know the value needs to change, like a counter. You'll rarely need the old var.

2The everyday types

TypeExampleFor
string"hello"text — always in quotes
number42, 3.14any number, whole or decimal
booleantrue / falseyes/no, on/off decisions
array[1, 2, 3]an ordered list (next section)
object{ id: 1 }a labelled record (next section)

3🎬 Try it — and meet template strings

Backticks `…` let you drop variables straight into text with ${...} — far nicer than gluing with +. Change the values and run:

variables & template strings
Ctrl+Enter
💡 typeof x tells you a value's type — handy when something behaves unexpectedly (a common bug is a number that's secretly a string like "3").
🧠

Quick check

1. You have a value that must never change. Which do you use?
const locks the value — reassigning it throws an error, which protects you from mistakes.
2. What is "3" (with quotes)?
Quotes make it a string. "3" + 1 gives "31", not 4 — a classic beginner trap.
3. `Hi ${name}` when name is "Sam" produces…
Backtick template strings substitute the variable inside ${ }.
🔓 You're reading a free chapter of JavaScript — the first two are open.
Unlock the rest of this course with a one-time payment.
Unlock this course →
🔓 See course prices