Variable declaration using var in Javascript - Time & Space Complexity
Let's see how declaring variables with var affects the speed of a program.
We want to know how the time to run changes when we declare variables using var.
Analyze the time complexity of the following code snippet.
var a = 5;
var b = 10;
var c = a + b;
console.log(c);
This code declares three variables using var and prints their sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Variable declarations and simple arithmetic.
- How many times: Each line runs once, no loops or repeats.
Since there are no loops or repeated steps, the time to run grows linearly with the number of variables we declare.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Time grows directly with the number of variable declarations.
Time Complexity: O(n)
This means the time grows in a straight line as you add more var declarations.
[X] Wrong: "Declaring variables with var takes constant time no matter how many variables."
[OK] Correct: Each variable declaration is a separate step, so more variables mean more steps and more time.
Understanding how simple statements like variable declarations affect time helps build a strong foundation for analyzing bigger code pieces.
"What if we replaced var with let or const? How would the time complexity change?"
Practice
var keyword do in JavaScript?Solution
Step 1: Understand
varbehaviorvardeclares variables that can be changed and redeclared in the same scope.Step 2: Compare with other declarations
Unlikeconstorlet,varis function-scoped and allows redeclaration.Final Answer:
Declares a variable that can be changed and redeclared -> Option CQuick Check:
varallows redeclaration = A [OK]
- Confusing var with const or let
- Thinking var is block-scoped
- Assuming var variables cannot be redeclared
var?Solution
Step 1: Recall correct
The correct syntax isvarsyntaxvar variableName = value;with no spaces or colons.Step 2: Check each option
Only var myVar = 10; matches the correct syntax exactly.Final Answer:
var myVar = 10; -> Option AQuick Check:
Correctvarsyntax = A [OK]
- Using 'variable' instead of 'var'
- Adding colons after var
- Adding spaces inside 'var'
var x = 5; var x = 10; console.log(x);
Solution
Step 1: Understand redeclaration with var
Usingvar, redeclaring a variable overwrites its previous value.Step 2: Trace the code execution
Firstxis 5, then redeclared as 10, soconsole.log(x)prints 10.Final Answer:
10 -> Option DQuick Check:
Redeclared var overwrites value = 10 [OK]
- Thinking redeclaration causes error
- Expecting first value to print
- Confusing var with let or const behavior
if (true) {
var a = 1;
}
console.log(a);Solution
Step 1: Understand var scope
varis function-scoped, not block-scoped, soais accessible outside the if block.Step 2: Check console.log output
Sinceais declared withvar,console.log(a)prints 1 without error.Final Answer:
No error, prints 1 -> Option BQuick Check:
varis function-scoped, so prints 1 [OK]
- Assuming var is block-scoped like let
- Expecting ReferenceError outside block
- Confusing var with const or let scope
function test() {
if (true) {
var count = 5;
}
var count = 10;
return count;
}
console.log(test());What will be the output and why?
Solution
Step 1: Analyze var declarations inside function
Bothvar countdeclarations are in the same function scope, so the second redeclaration overwrites the first.Step 2: Determine returned value
The function returns the last assigned value, which is 10.Final Answer:
10 because second var redeclares and overwrites -> Option AQuick Check:
Function-scoped var redeclaration overwrites = 10 [OK]
- Thinking redeclaration causes error
- Assuming block scope limits var
- Expecting first value to be returned
Ready to prove it?
Pass the test and this pattern moves unknown
