The global execution context is where your JavaScript code starts running. It sets up the environment so your code can work properly.
Global execution context in Javascript
// No direct syntax to write for global execution context // It is created automatically when your program runs
The global execution context is created automatically by JavaScript before your code runs.
It contains the global object (like window in browsers) and sets up the this keyword.
console.log('Hello, world!');
var x = 10; function greet() { console.log('Hi!'); } greet();
This program shows how the global execution context stores the variable name and the function sayName. When sayName() runs, it accesses name from the global context.
var name = 'Alice'; function sayName() { console.log(name); } sayName();
The global execution context is created once when your program starts.
All code outside functions runs inside this global context.
Understanding it helps you know where variables and functions live.
The global execution context is the first environment JavaScript creates to run your code.
It holds global variables, functions, and the global object.
All code outside functions runs here by default.
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
