Variable declaration using const in Javascript - Time & Space Complexity
Let's see how the time it takes to run code changes when we declare variables using const.
We want to know if declaring constants affects how long the program runs as it grows.
Analyze the time complexity of the following code snippet.
const a = 5;
const b = 10;
const c = a + b;
console.log(c);
This code declares three constants and prints their sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Variable declarations and a simple addition.
- How many times: Each line runs once, no loops or repeats.
Since there are no loops or repeated steps, the time grows linearly with the number of constants declared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The time grows directly with the number of declarations, but each is simple and quick.
Time Complexity: O(n)
This means the time grows in a straight line as you add more constant declarations.
[X] Wrong: "Declaring constants takes no time or is instant no matter how many."
[OK] Correct: Each declaration takes a small step, so more declarations mean more time, even if very small.
Understanding how simple statements like variable declarations affect time helps you think clearly about bigger code pieces.
"What if we replaced const with let or var? How would the time complexity change?"
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
