We use const to create variables that should not change. It helps keep our code safe and clear.
When you want to store a value that should never change, like a fixed number or a name.
When you want to avoid accidentally changing important data in your program.
When you want to make your code easier to understand by showing which values stay the same.
When working with values that represent constants, like configuration settings or math constants.
When you want to improve code reliability by preventing reassignment of variables.
Syntax
Javascript
const variableName = value;
The variable must be assigned a value when declared.
You cannot change the value of a const variable later.
Examples
This creates a constant named pi with the value 3.14.
Javascript
const pi = 3.14;
This creates a constant string variable name with the value "Alice".
Javascript
const name = "Alice";
This creates a constant boolean variable isActive set to true.
Javascript
const isActive = true;
Sample Program
This program creates a constant variable greeting and prints it. It shows that you cannot change the value later.
Javascript
const greeting = "Hello";
console.log(greeting);
// Trying to change the value will cause an error// greeting = "Hi"; // Uncommenting this line will cause an error
OutputSuccess
Important Notes
Even though you cannot reassign a const variable, if it holds an object or array, you can still change the contents inside.
Always use const by default, and only use let if you need to change the variable later.
Summary
const creates variables that cannot be reassigned.
It helps keep values safe and your code clear.
Use const for values that should stay the same.
Practice
(1/5)
1. What does the const keyword do in JavaScript?
easy
A. Declares a variable that cannot be reassigned
B. Declares a variable that can be reassigned anytime
C. Declares a function
D. Declares a variable only inside loops
Solution
Step 1: Understand const behavior
The const keyword creates a variable that cannot be reassigned after its initial value is set.
Step 2: Compare options
Only Declares a variable that cannot be reassigned correctly states that const variables cannot be reassigned.
Final Answer:
Declares a variable that cannot be reassigned -> Option A
Quick Check:
const means no reassignment [OK]
Hint: Remember: const means constant value, no changes allowed [OK]
Common Mistakes:
Thinking const variables can be reassigned
Confusing const with let or var
Assuming const declares functions
2. Which of the following is the correct syntax to declare a constant variable named pi with value 3.14?
easy
A. constant pi = 3.14;
B. const pi := 3.14;
C. const pi = 3.14;
D. let const pi = 3.14;
Solution
Step 1: Recall correct const syntax
In JavaScript, the correct syntax to declare a constant is const variableName = value;.
Step 2: Check each option
Only const pi = 3.14; matches the correct syntax. The other options use invalid keywords or symbols.
Final Answer:
const pi = 3.14; -> Option C
Quick Check:
Correct const syntax uses = and no extra keywords [OK]
Hint: Use const name = value; with equal sign [OK]