Lessons in this course (16)

3. Functions: declarations, expressions, arrows, default/rest/spread

Basic★ Must knowLesson 3 of 16
js
function add(a, b = 0) { return a + b; }            // declaration + default parameter
const mul = function (a, b) { return a * b; };       // expression
const square = x => x * x;                           // arrow function
const sum = (...nums) => nums.reduce((t, n) => t + n, 0);   // rest parameters
Math.max(...[3, 9, 2]);                              // spread into arguments

const copy = [...arr, 4];                            // spread array
const merged = { ...defaults, ...options };          // spread object (later wins)

// IIFE — immediately invoked function expression (old module pattern)
(function () { const secret = 1; })();

Arrow functions: no own this, arguments, super or prototype; can't be used with new; implicit return for single expressions. Great for callbacks; avoid as object methods that need this.

Higher-order functions take or return functions (map, filter, setTimeout, debounce). Pure functions return the same output for the same input with no side effects.