JAVASCRIPT FOUNDATIONS
Store values with variables
Variables give names to values. Good variable names make programs easier to read, update, and debug.
What you will learn
- Declare values with
constandlet - Choose the right declaration for a value
- Use clear names that explain intent
The concept
A variable connects a name to a value. Use const by default. Use let only when your program needs to assign a new value later.
Start with const. Change it to let only when reassignment is intentional.
Run this code
Change the role or starting count, then run the example in an isolated browser worker.
Frontend Developer: 3
How it works
- 1
rolenever receives a different value, so it usesconst. - 2
applicationsincreases, so it useslet. - 3
The template string combines both values into readable output.
Common mistake
const score = 1; score = 2; throws an error. Use let when reassignment is required.
Exercise
Track completed lessons
Create a variable named completedLessons, increase it by one, and print the result.
Interview connection
Be ready to explain scope, reassignment, and why const does not make an object deeply immutable.