LESSON 4
Constants
Learning objective: Use const for values that should never change.
Understand
Some values should stay fixed once set.
const declares a constant — a variable you cannot reassign. Use it by default and only reach for let when you truly need to change the value. This prevents accidental changes.
Analogy: A const is like writing in pen instead of pencil: it is meant to stay.
See It in Action
A constant value:
const PI = 3.14159;
// PI = 3; // this would cause an errorHow it works:
PI is fixed; attempting to reassign it throws an error, which protects important values.Try It Yourself
- Declare a
constfor your birth year. - Try reassigning it and read the error.
- Use it in a small calculation.
Quick Quiz
When should you prefer const?
Challenge
Use a constant for a value that must not change and explain why.
Success condition: Your code uses const correctly and reassigning it errors.
+10 XP
Lesson Complete
You answered correctly and completed the required practice. Your progress has been saved on this device.
Continue to next lesson →