JavaScript Keyed Collections
Explore Maps and Sets in JavaScript with interactive examples.
1. Defining Functions
Maps and Sets are keyed collections that offer powerful data storage options in JavaScript. There are several ways to define them.
Declaration
JavaScript Code
Live JavaScript representation with highlighted changes
// Function Declarationfunction square(number) {return number * number;}
Expression
JavaScript Code
Live JavaScript representation with highlighted changes
// Function Expressionconst square = function(number) {return number * number;};
Arrow Function
JavaScript Code
Live JavaScript representation with highlighted changes
// Arrow Functionconst square = (number) => number * number;
2. Recursion & the Call Stack
A function can call itself. This is called recursion. Let's visualize the call stack with a factorial function.
JavaScript Code
Live JavaScript representation with highlighted changes
function factorial(n) {if (n === 0 || n === 1) {return 1;}return n * factorial(n - 1);}factorial(5);
Call Stack
Result
Console Output