← Back to Week 4
Jan 28, 2026
Functional JavaScript: Callbacks & Array Methods
Lecture Notes
Slides: Functional JavaScript
Functions as Values
- Functions are first-class values in JavaScript — like arrays or numbers
- Can be assigned to variables, passed as parameters, stored in objects
- Parentheses
() execute the function; without them, you're passing the function itself
Function Declarations vs Expressions
- Declaration:
function greet(name) { return "Hello " + name; }
- Expression:
const greet = function(name) { return "Hello " + name; };
- Anonymous functions have no name — used when passing functions as values
Arrow Functions
- Shorter syntax:
const greet = (name) => { return "Hello " + name; };
- Implicit return:
const greet = (name) => "Hello " + name;
- Single parameter doesn't need parentheses:
const double = x => x * 2;
Callbacks
- A callback is a function passed to another function to be executed later
- Don't use
() when passing — that calls it immediately
- Example:
setTimeout(sayHello, 1000) — passes the function, doesn't call it
Array Iteration Methods
forEach(callback): Executes callback on each element (no return value)
map(callback): Returns new array with transformed values
filter(callback): Returns new array with elements where callback returns true
reduce(callback, initial): Reduces array to single value
Callback Parameters
- Array methods pass up to 3 arguments:
(item, index, array)
- You can use any or none of them
- Example:
nums.map((num, i) => num * i)
Key Concepts
- First-Class Functions: Functions can be treated like any other value
- Callbacks: Functions passed as arguments to be called later
- Arrow Functions: Concise syntax with
=>, implicit returns
- forEach: Loop through array, no return value
- map: Transform each element, returns new array
- filter: Keep elements that pass a test, returns new array
- reduce: Combine all elements into one value