Global execution context in Javascript - Time & Space Complexity
When JavaScript runs code, it creates a global execution context first. This context sets up the environment for the whole program.
We want to know how the time to create this global context changes as the code size grows.
Analyze the time complexity of the following code snippet.
const a = 10;
const b = 20;
function sum(x, y) {
return x + y;
}
const result = sum(a, b);
console.log(result);
This code declares variables and a function, then calls the function and logs the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated operations here.
- How many times: Each statement runs once during the global execution context creation.
As the number of statements grows, the time to set up the global context grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 setup steps |
| 100 | About 100 setup steps |
| 1000 | About 1000 setup steps |
Pattern observation: Doubling the code lines roughly doubles the setup time.
Time Complexity: O(n)
This means the time to create the global execution context grows in direct proportion to the number of statements.
[X] Wrong: "The global execution context runs instantly no matter how big the code is."
[OK] Correct: Even though it feels fast, the engine must process each line once, so more code means more setup time.
Understanding how the global execution context scales helps you explain how JavaScript prepares your code before running it. This skill shows you grasp the basics of how programs start.
"What if we added many function declarations inside the global context? How would the time complexity change?"
Practice
Solution
Step 1: Understand what global execution context is
The global execution context is the environment where all JavaScript code runs initially, outside any function.Step 2: Identify what it holds
It holds global variables, functions declared globally, and the global object (like window in browsers).Final Answer:
Global variables, functions, and the global object -> Option AQuick Check:
Global execution context = global variables + functions + global object [OK]
- Thinking it only holds local variables
- Confusing with function execution context
- Assuming it holds event listeners only
Solution
Step 1: Identify global variable declaration syntax
Usingvaroutside any function declares a global variable in JavaScript.Step 2: Check other options
function myVar() {}declares a function, not a variable.letandconstcan declare global variables but behave differently in scope and hoisting;const myVar;is invalid becauseconstneeds initialization.Final Answer:
var myVar = 10; -> Option DQuick Check:
Global variable declaration = var outside functions [OK]
- Using const without initialization
- Confusing function declaration with variable
- Assuming let always creates global variables
var x = 5;
function test() {
var x = 10;
console.log(x);
}
console.log(x);
test();
console.log(x);Solution
Step 1: Understand variable scopes
var x = 5;declares a global variable x with value 5. Insidetest(),var x = 10;declares a local variable x that shadows the global one.Step 2: Trace console.log outputs
Firstconsole.log(x);outside function prints global x = 5. Insidetest(),console.log(x);prints local x = 10. After callingtest(), the lastconsole.log(x);prints global x again, which is still 5.Final Answer:
5 10 5 -> Option CQuick Check:
Global x = 5, local x = 10 inside function [OK]
- Assuming global x changes after function call
- Confusing local and global scopes
- Expecting all outputs to be same
console.log(a); var a = 10;
Solution
Step 1: Understand hoisting of var declarations
In JavaScript,vardeclarations are hoisted to the top of their scope with an initial value ofundefined.Step 2: Trace the console.log output
Atconsole.log(a);, variableaexists but is not yet assigned 10, so it printsundefined.Final Answer:
undefined because of hoisting of 'var a' -> Option AQuick Check:
var hoisting = undefined before assignment [OK]
- Expecting ReferenceError instead of undefined
- Thinking variable is initialized before console.log
- Confusing var with let/const hoisting
var count = 1;
function increment() {
count++;
var count = 10;
console.log(count);
}
increment();
console.log(count);
What will be the output and why?Solution
Step 1: Analyze variable hoisting inside function
Insideincrement(),var countis hoisted to the top of the function scope but not initialized yet, so it starts asundefined.Step 2: Evaluate the increment operation
The linecount++;tries to increment the localcountwhich isundefined, setting it toNaN. Thencount = 10;assigns 10, andconsole.log(count);prints 10.Step 3: Check global count after function call
The globalcountremains unchanged at 1, soconsole.log(count);outside prints 1.Final Answer:
NaN 1 -> Option BQuick Check:
Local var hoisted undefined -> NaN on increment -> 10 on assignment [OK]
- Thinking the output is NaN 1 because of undefined++
- Assuming increment affects global count
- Ignoring hoisting of local var
Ready to prove it?
Pass the test and this pattern moves unknown
