Variables
Prerequisites: JavaScript syntax
A variable is a named place to store a value so you can use or change it later.
Real-world analogy: a variable is like a labeled box — write a label on it, put something inside, and read or replace the contents later by referring to the label.
Syntax
let name = "Ada";
const age = 25;
Example
let score = 10;
console.log(score);
score = 20;
console.log(score);
Expected output
10
20
Use let for a variable you'll reassign, and const for one you won't
— prefer const by default, and switch to let only when you actually
need to change the value. Avoid the older var keyword; it has
confusing scoping rules that let/const fix.
Common mistakes
- Trying to reassign a
constvariable —TypeError: Assignment to constant variable. - Using a variable before declaring it.
Practice
Create a variable called city holding your city's name, then log it.
Show solution
const city = "Lagos";
console.log(city);