What if you could stop bugs before they happen by locking your values?
Why Variable declaration using const in Javascript? - Purpose & Use Cases
Imagine you are writing a program where you need to store a value that should never change, like the number of days in a week. You write the value once and then accidentally change it later in your code.
Without a way to lock a value, you might accidentally overwrite important data. This can cause bugs that are hard to find because the program behaves unpredictably when values change unexpectedly.
Using const lets you declare variables that cannot be reassigned. This means once you set a value, the program will prevent you from changing it, keeping your data safe and your code more reliable.
let daysInWeek = 7; daysInWeek = 8; // Oops, changed by mistake!
const daysInWeek = 7; daysInWeek = 8; // Error: Assignment to constant variable.
It enables you to write safer code by protecting important values from accidental changes.
Think of const like labeling a jar as 'Do Not Open' so no one accidentally changes what's inside, like setting the speed limit on a road that should never change.
const prevents variables from being reassigned.
It helps avoid bugs caused by accidental changes.
Using const makes your code clearer and safer.
Practice
const keyword do in JavaScript?Solution
Step 1: Understand
Theconstbehaviorconstkeyword 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 thatconstvariables cannot be reassigned.Final Answer:
Declares a variable that cannot be reassigned -> Option AQuick Check:
constmeans no reassignment [OK]
- Thinking const variables can be reassigned
- Confusing const with let or var
- Assuming const declares functions
pi with value 3.14?Solution
Step 1: Recall correct const syntax
In JavaScript, the correct syntax to declare a constant isconst variableName = value;.Step 2: Check each option
Onlyconst pi = 3.14;matches the correct syntax. The other options use invalid keywords or symbols.Final Answer:
const pi = 3.14; -> Option CQuick Check:
Correct const syntax uses = and no extra keywords [OK]
const name = value; with equal sign [OK]- Using := instead of =
- Writing 'constant' instead of 'const'
- Combining let and const keywords
const greeting = 'Hello'; greeting = 'Hi'; console.log(greeting);
Solution
Step 1: Understand reassignment of const variable
The variablegreetingis declared withconst, so it cannot be reassigned after initialization.Step 2: Analyze the reassignment line
Trying to assign 'Hi' togreetingcauses a TypeError at runtime.Final Answer:
TypeError -> Option DQuick Check:
Reassigning const variable throws TypeError [OK]
- Assuming const variables can be changed
- Expecting console.log to print new value
- Confusing TypeError with SyntaxError
const number; number = 10; console.log(number);
Solution
Step 1: Check const declaration rules
When declaring aconstvariable, it must be initialized with a value immediately.Step 2: Identify the problem in code
The code declaresconst number;without assigning a value, which causes a SyntaxError.Final Answer:
Missing value when declaring const variable -> Option AQuick Check:
const must be initialized on declaration [OK]
- Declaring const without initialization
- Thinking console.log causes error
- Assuming variable names like 'number' are invalid
const and why?const colors = ['red', 'green', 'blue'];
colors.push('yellow');
console.log(colors);Solution
Step 1: Understand const with objects and arrays
Usingconstmeans the variable cannot be reassigned, but the contents of objects or arrays can be changed.Step 2: Analyze the code behavior
The arraycolorsis declared withconst, butpushmodifies its contents without reassigning the variable, so it works fine.Final Answer:
Use const for the array; you can change contents but not reassign the variable -> Option BQuick Check:
const protects variable binding, not object contents [OK]
- Thinking const makes array immutable
- Using let when const is better for fixed references
- Confusing reassignment with content modification
Ready to prove it?
Pass the test and this pattern moves unknown
