Functions - Level II
Once you can declare and call a basic function, the next step is learning the features that make functions flexible: default parameters, rest parameters, arrow functions, and the idea that functions are values you can store and pass around. These features appear constantly in modern JavaScript, from array methods to React components, so they are worth getting comfortable with.
Default and rest parameters
A default parameter supplies a value when the caller leaves an argument out or passes undefined. A rest parameter, written with three dots, gathers any remaining arguments into a real array. Rest must be the last parameter.
function greet(name = 'Guest', greeting = 'Hello') {
return `${greeting}, ${name}!`;
}
greet(); // "Hello, Guest!"
greet('Asha', 'Hi'); // "Hi, Asha!"
greet(undefined, 'Welcome'); // "Welcome, Guest!"
function total(taxRate, ...prices) {
const sum = prices.reduce((acc, p) => acc + p, 0);
return sum * (1 + taxRate);
}
total(0.18, 100, 250, 50); // 472
const nums = [4, 9, 2];
Math.max(...nums); // 9, spread passes array items as arguments
Rest parameters replace the older arguments object, which looks like an array but lacks array methods and does not exist in arrow functions.
Arrow functions
Arrow functions are a shorter way to write function expressions. With a single expression after the arrow, the result is returned automatically: const square = n => n * n;. With braces, you need an explicit return. To return an object literal directly, wrap it in parentheses: () => ({ ok: true }).
Arrows also differ in behaviour. They do not have their own this; they use the this of the surrounding code. That makes them ideal for callbacks inside methods, but unsuitable as object methods that need this to refer to the object. They cannot be used with new.
Functions are values
In JavaScript a function can be stored in a variable, kept in an array or object, passed as an argument and returned from another function. A function that takes or returns another function is called a higher-order function. You already use them: map, filter, setTimeout and addEventListener all accept functions.
function repeat(times, action) {
for (let i = 0; i < times; i++) action(i);
}
repeat(3, i => console.log('Round', i + 1));
Declarations versus expressions and hoisting
A function declaration such as function greet() {} is hoisted, meaning you can call it earlier in the file than where it is written. A function expression assigned to const, including any arrow function, is not usable before its line runs. Calling it too early throws "Cannot access before initialization".
Traps with these features
Default values apply only to undefined. Passing null or 0 keeps that value, so greet(null) returns "Hello, null!".
Defining an object method as an arrow, such as const user = { name: 'Asha', hi: () => this.name }, returns undefined because the arrow does not get the object as this. Use shorthand method syntax, hi() { return this.name; }, for methods.
Destructuring in the parameter list is another everyday pattern. Writing function createUser({ name, role = 'student' }) {} lets callers pass a single options object with named fields, so the order of arguments no longer matters.
Key ideas
- Defaults fill in missing arguments; rest collects extras into an array.
- Arrow functions are concise and borrow
thisfrom their surroundings. - Functions are values that can be passed to and returned from other functions.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.