Understanding Function Execution Context in JavaScript
📖 Scenario: Imagine you are a chef in a kitchen. Each time you start cooking a new dish, you prepare your workspace with all the ingredients and tools you need. This workspace is like a function's execution context in JavaScript, where everything needed to run the function is set up.
🎯 Goal: You will create a simple JavaScript function and explore how its execution context works by defining variables inside it and printing their values.
📋 What You'll Learn
Create a function named cookDish.
Inside the function, declare a variable ingredient with the value 'Tomato'.
Declare another variable quantity with the value 3 inside the function.
Return a string combining quantity and ingredient in the format: '3 Tomatoes'.
Call the function and print the returned string.
💡 Why This Matters
🌍 Real World
Functions are like mini-workspaces in programming where you keep all the tools and ingredients needed to complete a task. Understanding their execution context helps you write clear and bug-free code.
💼 Career
Knowing how function execution context works is essential for debugging, writing clean code, and understanding how JavaScript runs your programs in web development and software engineering.
Progress0 / 4 steps
1
Create the function cookDish with no content
Write a function named cookDish using the function keyword with empty curly braces {}.
Javascript
Hint
Use the syntax: function cookDish() { } to create an empty function.
2
Add variables ingredient and quantity inside cookDish
Inside the cookDish function, declare a variable ingredient and set it to 'Tomato'. Also declare a variable quantity and set it to 3.
Javascript
Hint
Use let ingredient = 'Tomato'; and let quantity = 3; inside the function.
3
Return a string combining quantity and ingredient
Inside the cookDish function, add a return statement that returns a string combining quantity and ingredient in the format '3 Tomatoes'. Use a template literal with backticks and ${}.
Javascript
Hint
Use return `${quantity} ${ingredient}s`; to create the string.
4
Call cookDish and print the result
Call the function cookDish() and print its returned value using console.log.
Javascript
Hint
Use console.log(cookDish()); to print the returned string.
Practice
(1/5)
1. What does the function execution context in JavaScript primarily keep track of?
easy
A. Variables and parameters inside the function
B. The browser window size
C. The user's keyboard input globally
D. The network requests made by the page
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 A
Quick Check:
Execution context tracks variables and parameters [OK]
Hint: Execution context tracks function's own variables and parameters [OK]
Common Mistakes:
Thinking it tracks global browser info
Confusing with event listeners
Assuming it tracks network requests
2. Which of the following is the correct way to define a function in JavaScript that creates a new execution context when called?
easy
A. var greet = 'Hi';
B. function greet() { console.log('Hi'); }
C. console.log('Hi');
D. if (true) { console.log('Hi'); }
Solution
Step 1: Identify function declaration syntax
Only the syntax starting with the keyword function defines 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 B
Quick Check:
Function declaration uses function keyword [OK]
Hint: Look for the 'function' keyword to define a function [OK]
Common Mistakes:
Confusing variable assignment with function definition
Thinking console.log alone defines a function
Using if statement as a function
3. What will be the output of the following code?
function test() {
let x = 5;
return x + 3;
}
console.log(test());
medium
A. undefined
B. 5
C. 8
D. Error
Solution
Step 1: Analyze the function body
The function test declares a variable x with value 5 inside its execution context.
Step 2: Calculate the return value
The function returns x + 3, which is 5 + 3 = 8.
Final Answer:
8 -> Option C
Quick Check:
Return value is 5 + 3 = 8 [OK]
Hint: Add local variable and return value inside function [OK]
Common Mistakes:
Confusing return value with variable value
Expecting undefined because of missing global variable
Thinking function does not return anything
4. Identify the error in the following code related to function execution context:
function add() {
console.log(sum);
let sum = 10 + 5;
}
add();
medium
A. TypeError because sum is not a function
B. No error, outputs 15
C. SyntaxError due to missing return statement
D. ReferenceError because 'sum' is used before declaration
Solution
Step 1: Understand variable hoisting with let
Variables declared with let are not hoisted like var. Accessing them before declaration causes ReferenceError.
Step 2: Check the console.log usage
The code tries to log sum before it is declared, causing ReferenceError.
Final Answer:
ReferenceError because 'sum' is used before declaration -> Option D
Quick Check:
Using let variable before declaration causes ReferenceError [OK]
Hint: Don't access let variables before declaration inside function [OK]
Common Mistakes:
Assuming let variables are hoisted like var
Expecting output 15 without error
Confusing ReferenceError with SyntaxError
5. Consider this code:
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?
hard
A. Inner function can access variables of outer function's execution context
B. Inner function cannot access outer function variables
C. Outer function loses its execution context after returning
D. Variables declared in inner function are global
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 returns a + b where a is from outer context and b is local, showing access to outer variables.
Final Answer:
Inner function can access variables of outer function's execution context -> Option A
Quick Check:
Closures allow inner functions to access outer variables [OK]
Hint: Inner functions access outer variables via closure [OK]
Common Mistakes:
Thinking inner function variables are global
Assuming outer context is lost immediately
Believing inner cannot access outer variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.