📖 Scenario: You are creating a simple program to store information about a favorite fruit. You want to make sure the fruit name does not change accidentally.
🎯 Goal: Learn how to declare a variable using const in JavaScript and understand that its value cannot be changed later.
📋 What You'll Learn
Declare a variable using const
Assign a string value to the const variable
Try to change the value (to see what happens, but this will be commented out)
Print the value of the const variable
💡 Why This Matters
🌍 Real World
Using <code>const</code> helps keep important values safe from accidental changes in programs, like fixed settings or names.
💼 Career
Understanding <code>const</code> is essential for writing reliable JavaScript code in web development and many programming jobs.
Progress0 / 4 steps
1
Declare a const variable
Declare a variable called favoriteFruit using const and assign it the value "Apple".
Javascript
Hint
Use const favoriteFruit = "Apple"; to declare the variable.
2
Add a comment about changing the variable
Add a comment below the declaration explaining that favoriteFruit cannot be changed because it is declared with const.
Javascript
Hint
Write a comment starting with // explaining the variable is constant.
3
Try to change the const variable (commented out)
Add a line below the comment that tries to change favoriteFruit to "Banana", but comment it out so the program does not break.
Javascript
Hint
Write // favoriteFruit = "Banana"; to show the change attempt is commented out.
4
Print the value of the const variable
Write a line to print the value of favoriteFruit using console.log.
Javascript
Hint
Use console.log(favoriteFruit); to print the value.
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]