Coding Manifestation Logo
Lesson 6

Constants

Constants are variables whose values cannot be changed once assigned.

const PI = 3.14159;Value cannot be changed

Constants are declared using the const keyword. Once a value is assigned to a constant, it cannot be reassigned.

1const PI = 3.14159;2const MAX_USERS = 100;3const APP_NAME = "Master DSA";
  • Must be initialized at declaration.
  • Value cannot be reassigned.
  • Use UPPER_SNAKE_CASE for constants.
  • Improves code reliability and readability.
  • const PI = 3.14159;
  • const MAX_LIMIT = 1000;
  • const APP_NAME = "Master DSA";
  • const isLoggedIn = true;
Use meaningful names in UPPER_SNAKE_CASE for values that don’t change.
  • PI = 3.14159;
    // Missing const
  • const MAX = 10;MAX = 20;
    // Reassignment
  • const userName = "Vivek";userName = "Kumar";
    // Reassignment
  • Mathematical values
    like PI, E, GRAVITY
  • Configuration values
    like API_URL, TIMEOUT
  • Limits and sizes
    like MAX_USERS, MAX_FILE_SIZE

Modify the code and click Run to see the result.

JavaScript
Output
Your output will appear here...

Use const by default. Only use let when you know the value will change later.

const
let
PrevNext