› Module 3: Functions › 3.3 The Call Stack
M O D U L E

Module 3: Functions

Reusable, first-class blocks of behavior

3.1 Function Basics

A function is a reusable block of code designed to perform a dedicated computation or action. In JavaScript, functions are first-class citizens: they can be treated as first-class objects, stored in variables, passed into other functions as callbacks, and returned as values.

Figure 3.1: Functions as First-Class Citizens in JavaScript 1. Stored in Variables const fn = function() {} Treated as a regular value 2. Passed as Callbacks arr.map(x => x * 2) Higher-order functions 3. Returned as Values return function() {} Closures & factories

Figure 3.1: The three capabilities that make JavaScript functions first-class citizens in computer science.

3.2 Function Expressions & Arrow Functions

Arrow functions (=>) introduced in ES6 provide a concise syntax and lexical scoping behavior that fundamentally changes how the this keyword is resolved.

Figure 3.2: Regular Functions vs. Arrow Functions Internals Regular Function • Dynamic 'this' determined at call time • Has built-in 'arguments' object • Can be constructor with 'new' Arrow Function (=>) • Lexical 'this' (inherited from parent scope) • No 'arguments' (use rest ...args) • CANNOT be used with 'new'

Figure 3.2: Architectural differences between standard functions and arrow functions.

3.3 The Call Stack

The Call Stack is a LIFO (Last In, First Out) data structure used by the JavaScript engine to keep track of execution contexts and return addresses as functions call each other.

Figure 3.3: JavaScript Call Stack Execution Mechanism (LIFO) // Code execution flow function sayHi() { console.log("Hi"); } function greet() { sayHi(); } greet(); // Initiated in global Call Stack (LIFO) sayHi() [Executing] greet() [Waiting] Global Execution Context

Figure 3.3: Call stack frames pushed in calling order. The top frame completes first and is popped off before the caller resumes.

3.4 Closures

A closure is a function bundled together with references to its surrounding lexical environment. In JavaScript, closures allow an inner function to retain persistent access to an outer function's scope variables, even after the outer function has finished executing and returned.

Figure 3.4: Lexical Closure Memory Binding Mechanism outer() Scope (Closed & Popped) let count = 0; // Private state return function inner() { ... } Lives in memory via Closure link Remembers counter() Function Instance const counter = outer(); counter(); // count = 1 counter(); // count = 2

Figure 3.4: Closure preserves access to outer variables even after outer() has exited the call stack.

3.5 The 'this' Keyword

In JavaScript, the this keyword refers to the current execution context object. Crucially, the value of this is determined dynamically at call time by HOW a function is called, not where it was declared (with the sole exception of arrow functions).

Figure 3.5: How JavaScript Determines the 'this' Context 'this' Regular Function greet() → window / undefined (strict) Object Method user.greet() → The 'user' object Arrow Function () => { ... } → Lexical surrounding scope

Figure 3.5: How 'this' is resolved depending on execution context: standalone invocation, object method call, or lexical inheritance via arrow functions.

MODULE COMPREHENSIVE QUIZ

Module 3 Final Quiz

Test your understanding across all 5 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 3 Final Quiz →
Yukti Mascot