Detailed explanations & code examples are in the JavaScript (.js) file
Click the { } button at the bottom right to view it
Open the browser console (F12) to see all outputs!
.forEach()
Iterates over array elements and applies a function to each one
array.forEach(callback)| Example | Function | What It Does |
|---|---|---|
| Example 1 | double, triple, square, cube | Math operations on numbers |
| Example 2 | upperCase, lowercase, capitalize | Text transformations on strings |
.map()
Creates a NEW array by applying a function to each element
const newArray = array.map(callback)| Example | Input | Transformation | Output |
|---|---|---|---|
| Example 1 | [1, 2, 3, 4, 5] | square / cube | [1, 4, 9, 16, 25] |
| Example 2 | ["Umer", "Azmi"] | toUpperCase | ["UMER", "AZMI"] |
| Example 3 | ["2024-1-10"] | Format date | ["1/10/2024"] |
.filter()
Creates a NEW array with only elements satisfiying a condition
const newArray = array.filter(callback)| Example | Input | Filter Condition | Output |
|---|---|---|---|
| Example 1 | [1,2,3,4,5,6,7] | isEven (% 2 === 0) | [2, 4, 6] |
| Example 2 | [16,17,18,19,20,65] | isAdult (>= 18) | [18, 19, 20, 65] |
| Example 3 | ["apple","pomegranate"] | Short (<= 6 chars) | ["apple"] |
.reduce()
Reduces an array to a SINGLE value
const result = array.reduce(callback)| Example | Input Array | Operation | Result |
|---|---|---|---|
| Example 1 | [5, 30, 10, 25, 15, 20] | sum (add all) | 105 |
| Example 2 | [75, 50, 90, 80, 65, 95] | getMax | 95 |
| Example 2 | [75, 50, 90, 80, 65, 95] | getMin | 50 |
| Method | Returns | Modifies Original? | Use Case |
|---|---|---|---|
| forEach() | undefined | ✓ Yes | Perform action on each element |
| map() | New array | ✗ No | Transform each element |
| filter() | New array | ✗ No | Select elements that matches a condition |
| reduce() | Single value | ✗ No | Combine elements into one value |
Defining functions as values stored in variables
function myFunction() { return "Hello"; }const myFunction = function() { return "Hello"; };
const squares = numbers.map(function(element){ return Math.pow(element, 2); }); setTimeout(function(){ console.log("function expression: Hello"); }, 3000);
Concise syntax for writing function expressions
(parameters) => expressionconst square = function(x) { return x * x; };const square = (x) => x * x;
// Multiple lines (need brackets) const hello = (name, age) => { console.log(`Hello ${name}`); console.log(`You are ${age} years old`); }; // Single line (implicit return) const squares = numbers.map(element => Math.pow(element, 2)); const evenNums = numbers.filter((element) => element % 2 === 0); const total = numbers.reduce((acc, el) => acc + el);
| Parameters | Syntax | Example |
|---|---|---|
| No parameters | () => code |
() => console.log("Hi") |
| One parameter | x => code |
x => x * 2 |
| Multiple parameters | (x, y) => code |
(x, y) => x + y |
| One line | x => x * 2 |
Implicit return |
| Multiple lines | x => { return x * 2; } |
Explicit return needed |
Loading...