A variable gives a name to a value so you can use it later. In modern JavaScript you create variables with let or const.
const name = "Dara";
let score = 10;
score = score + 5;
console.log(name, score);let or const?
constcannot be reassigned. Use it by default.letcan be reassigned. Use it when the value changes, like a counter.- Avoid the old
varkeyword in new code.
Code examples
let count = 0;
count = count + 1;
count = count + 1;
console.log(count);Expected output
2
Common mistakes
Common mistake: Reassigning a const
Assigning a new value to a const throws "TypeError: Assignment to constant variable." Use let if the value needs to change.
Check your understanding
3 quick questions about this lesson.