1. JavaScript Execution Contexts
Call Stack
The call stack is a LIFO (last in, first out) data structure that manages execution contexts created
during a program's run. Each time a function is invoked, a new execution context is created and
placed on the call stack.
// Example: Simple Call Stack
function first() {
second();
console.log('Returning to first');
}
function second() {
third();
console.log('Returning to second');
}
function third() {
console.log('In third function');
}
first();
MDN: Call Stack
Global Execution Context
The global execution context is the default context where the JavaScript code runs. It creates a
global object (window in browsers) and defines the outer scope.
console.log(window); // In browsers, window is the global object.
MDN: Execution Context
Functional Execution Context
Every time a function is invoked, a new execution context is created for that function. This context
includes its own variable environment, scope chain, and a reference to the global object.
function greet(name) {
let greeting = 'Hello, ' + name;
return greeting;
}
console.log(greet('World'));
3. The History Object
Methods
-
history.back(): Navigates to the previous page in session history.
-
history.forward(): Navigates to the next page in session history.
-
history.go(n): Loads a specific page in the session history relative to the current
page (negative numbers go backward, positive go forward).
// Navigate back:
history.back();
// Navigate forward:
history.forward();
// Navigate two pages back:
history.go(-2);
MDN: History