Latticework

Command Palette

Search for a command to run...

JavaScript

Functions

14 min

Explanation

Arrow functions ((args) => expression) are a shorter alternative to function — for a single expression body, the value is returned automatically with no return keyword or {} needed. Default parameters ((factor = 2)) supply a fallback value used only when that argument is omitted (or passed as undefined) — not for 0, null, or any other "falsy" value. Array methods like .map() return a brand-new array rather than modifying the original, which is why they're a common building block for "transform this list" logic.

const multiplyAll = (nums, factor = 2) => nums.map((n) => n * factor);
console.log(multiplyAll([1, 2, 3]));
// [2, 4, 6]
Try it

greet() with zero arguments falls back to the default; greet('Grace') overrides it. .map() never touches the original [1,2,3] array — it always returns a new one.

Loading editor…
Exercise

Write `multiplyAll`, an arrow function assigned to a `const`, taking `(nums, factor = 2)` — `factor` defaults to 2 if not given. Return a NEW array with every number multiplied by `factor` (don't mutate `nums`).

Quiz

In `const greet = (name = "friend") => ...`, what happens if you call greet() with no arguments?

Checkpoint

You can write arrow functions with default parameters and use array methods like map to transform data without mutating it.