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 error
How it works: PI is fixed; attempting to reassign it throws an error, which protects important values.

Try It Yourself

  1. Declare a const for your birth year.
  2. Try reassigning it and read the error.
  3. 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.