Menu
Coddy logo textTech

Constants

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 11 of 77.

In JavaScript, sometimes you need variables that never change. This is where const comes in! It’s used to create constants—values that stay the same after being set.

Use the const keyword, just like let, but once you set its value, you cannot change it later.

const pi = 3.14;
console.log(pi); // Output: 3.14

// Trying to change pi will cause an error:
pi = 3.14159; // Error: Assignment to constant variable.

Use const for values that should never change, like:

  • Mathematical constants (pi = 3.14)
  • Fixed settings (maxUsers = 100)

By using const, your code is safer and easier to understand!

challenge icon

Challenge

Beginner

Initialize a constant variable named minSize with the value of 50

Cheat sheet

Use const to create variables that never change:

const pi = 3.14;
console.log(pi); // Output: 3.14

Once set, const variables cannot be reassigned:

pi = 3.14159; // Error: Assignment to constant variable.

Use const for:

  • Mathematical constants (pi = 3.14)
  • Fixed settings (maxUsers = 100)

Try it yourself

// Type your code below


// Don't change the line below
console.log(`minSize = ${minSize}`)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals