Lesson 6
Constants
Constants are variables whose values cannot be changed once assigned.
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.
- // Missing const
PI = 3.14159; - // Reassignment
const MAX = 10;MAX = 20; - // Reassignment
const userName = "Vivek";userName = "Kumar";
- Mathematical valueslike PI, E, GRAVITY
- Configuration valueslike API_URL, TIMEOUT
- Limits and sizeslike 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.
constlet