Global scope in Javascript - Time & Space Complexity
Let's explore how using global variables affects the time complexity of JavaScript code.
We want to see if accessing or modifying global variables changes how long the program takes as input grows.
Analyze the time complexity of the following code snippet.
let count = 0; // global variable
function incrementArray(arr) {
for (let i = 0; i < arr.length; i++) {
count += arr[i];
}
return count;
}
const numbers = [1, 2, 3, 4, 5];
incrementArray(numbers);
This code sums all numbers in an array and adds them to a global variable called count.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop that goes through each item in the array.
- How many times: The loop runs once for every element in the array.
As the array gets bigger, the loop runs more times, doing more additions to the global variable.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the size of the input array.
Time Complexity: O(n)
This means the time to run the function grows in a straight line as the input array gets bigger.
[X] Wrong: "Using a global variable makes the code slower because it adds extra work each time."
[OK] Correct: Accessing or updating a global variable is a simple operation and does not add extra loops or repeated work that grows with input size.
Understanding how global variables affect performance helps you write clear and efficient code, a skill valued in real projects and interviews.
"What if the function called itself recursively instead of using a loop? How would the time complexity change?"
Practice
global scope in JavaScript?Solution
Step 1: Understand global scope meaning
Global scope means a variable is declared outside any function and is accessible everywhere in the code.Step 2: Compare options with definition
Only The variable can be accessed anywhere in the code. correctly states that the variable can be accessed anywhere.Final Answer:
The variable can be accessed anywhere in the code. -> Option AQuick Check:
Global scope = accessible everywhere [OK]
- Thinking global variables are only inside functions
- Confusing global with local scope
- Believing global variables disappear after function ends
Solution
Step 1: Identify where to declare global variables
Global variables must be declared outside any function or block.Step 2: Check declaration keywords
Usingvaroutside functions creates a global variable. Options A, B, and D declare inside functions or blocks, so they are not global.Final Answer:
Outside any function using var -> Option BQuick Check:
Global variable = declared outside functions [OK]
- Declaring variables inside functions and expecting global scope
- Using let or const inside blocks thinking they are global
- Confusing block scope with global scope
var x = 10;
function show() {
console.log(x);
}
show();Solution
Step 1: Identify variable scope
Variablexis declared globally withvarand has value 10.Step 2: Check function access
The functionshowlogsx. Sincexis global, it is accessible inside the function.Final Answer:
10 -> Option AQuick Check:
Global variable accessed inside function = 10 [OK]
- Expecting undefined because variable is outside function
- Thinking console.log will cause ReferenceError
- Confusing local and global scope
function test() {
var a = 5;
}
console.log(a);Solution
Step 1: Check variable scope
Variableais declared inside functiontestwithvar, so it is local to that function.Step 2: Access variable outside function
Trying to logaoutside the function causes a ReferenceError becauseais not in global scope.Final Answer:
ReferenceError: a is not defined -> Option CQuick Check:
Local variable accessed globally = ReferenceError [OK]
- Assuming var inside function is global
- Expecting output 5 instead of error
- Confusing ReferenceError with SyntaxError
Solution
Step 1: Identify global variable declaration
var counter = 0; function count() { counter++; console.log(counter); }declarescounteroutside the function, making it global and accessible inside the function.Step 2: Check counter increment behavior
Insidecount,counter++increases the global variable, so the count persists across calls.Final Answer:
var counter = 0; function count() { counter++; console.log(counter); } -> Option DQuick Check:
Global counter increments inside function =var counter = 0; function count() { counter++; console.log(counter); }[OK]
- Declaring counter inside function resets it each call
- Using let inside function hides global variable
- Expecting local counter to remember previous value
Ready to prove it?
Pass the test and this pattern moves unknown
