Function execution context is how JavaScript keeps track of what to do when a function runs. It helps the computer remember variables and where it is inside the function.
Function execution context in Javascript
// No direct syntax to write for execution context, it happens automatically when a function runs function example() { // code here } example();
Execution context is created automatically when a function is called.
It stores information like variables, parameters, and the place in code where the function was called.
greet runs, a new execution context is created to hold name and message.function greet(name) { let message = `Hello, ${name}!`; console.log(message); } greet('Alice');
add runs, it creates its own execution context with a and b.function add(a, b) { return a + b; } let sum = add(3, 4); console.log(sum);
This program shows how the function multiply creates an execution context to store x, y, and result. The values inside the function are separate from outside.
function multiply(x, y) { let result = x * y; console.log(`Inside function: result = ${result}`); return result; } let output = multiply(5, 6); console.log(`Outside function: output = ${output}`);
Each function call creates a new execution context, even if the same function is called multiple times.
Variables inside a function are not visible outside unless returned or passed out.
Understanding execution context helps with debugging and writing better code.
Function execution context is created automatically when a function runs.
It keeps track of variables and parameters inside the function.
Helps JavaScript know what to do step-by-step inside functions.
Practice
Solution
Step 1: Understand what execution context means
The function execution context is created when a function runs and it keeps track of what happens inside that function.Step 2: Identify what is tracked inside the function
It specifically keeps track of variables and parameters declared or passed inside the function.Final Answer:
Variables and parameters inside the function -> Option AQuick Check:
Execution context tracks variables and parameters [OK]
- Thinking it tracks global browser info
- Confusing with event listeners
- Assuming it tracks network requests
Solution
Step 1: Identify function declaration syntax
Only the syntax starting with the keywordfunctiondefines a function that creates an execution context when called.Step 2: Check other options
Options B, C, and D do not define functions; they are variable assignment or direct statements.Final Answer:
function greet() { console.log('Hi'); } -> Option BQuick Check:
Function declaration usesfunctionkeyword [OK]
- Confusing variable assignment with function definition
- Thinking console.log alone defines a function
- Using if statement as a function
function test() {
let x = 5;
return x + 3;
}
console.log(test());Solution
Step 1: Analyze the function body
The functiontestdeclares a variablexwith value 5 inside its execution context.Step 2: Calculate the return value
The function returnsx + 3, which is 5 + 3 = 8.Final Answer:
8 -> Option CQuick Check:
Return value is 5 + 3 = 8 [OK]
- Confusing return value with variable value
- Expecting undefined because of missing global variable
- Thinking function does not return anything
function add() {
console.log(sum);
let sum = 10 + 5;
}
add();Solution
Step 1: Understand variable hoisting with let
Variables declared withletare not hoisted likevar. Accessing them before declaration causes ReferenceError.Step 2: Check the console.log usage
The code tries to logsumbefore it is declared, causing ReferenceError.Final Answer:
ReferenceError because 'sum' is used before declaration -> Option DQuick Check:
Using let variable before declaration causes ReferenceError [OK]
- Assuming let variables are hoisted like var
- Expecting output 15 without error
- Confusing ReferenceError with SyntaxError
function outer() {
let a = 10;
function inner() {
let b = 20;
return a + b;
}
return inner();
}
console.log(outer());What does this example demonstrate about function execution contexts?
Solution
Step 1: Understand nested function execution contexts
The inner function creates its own execution context but can access variables from the outer function's context due to closure.Step 2: Analyze the return value
The inner function returnsa + bwhereais from outer context andbis local, showing access to outer variables.Final Answer:
Inner function can access variables of outer function's execution context -> Option AQuick Check:
Closures allow inner functions to access outer variables [OK]
- Thinking inner function variables are global
- Assuming outer context is lost immediately
- Believing inner cannot access outer variables
Ready to prove it?
Pass the test and this pattern moves unknown
