Functions
A function is a reusable body of code you can call with arguments. In JavaScript, functions are values: you can store them, pass them, and return them from other functions.
Detailed Explanation
A function packages work under a name (or no name). You call it, it receives arguments, it runs, and it returns a value. If you write no return, the return value is undefined. return without an expression also yields undefined.
Functions are first-class values. That single fact explains callbacks, closures, array methods, and half of the language's APIs. const greet = function (name) { return hi ${name}; } stores a function. setTimeout(greet, 0, "Ada") passes one. A function that takes or returns a function is a higher-order function; array.map is the textbook case.
There are several ways to create one. A declaration (function sum(a, b) { return a + b; }) is hoisted as a complete function, so you can call it above its line. A function expression assigns a function value to a name and is not callable before that assignment runs. An arrow function is a shorter expression with different this rules. A method is a function stored as an object property. A constructor is a function you call with new.
Parameters are local names bound to the arguments you pass. Extra arguments are ignored by the parameter list (they still appear on arguments in non-arrow functions). Missing arguments are undefined. Default parameters fill those in. Rest parameters collect leftovers into an array.
JavaScript does not check types or arity. sum(1) is NaN if b is used in addition, not a call-time error. That flexibility is why JS APIs often accept one argument or two, and why typos in argument order fail late.
arguments is the array-like object of every passed value, available in function functions only. Prefer rest parameters. arguments is not a real array, does not exist in arrows, and in strict mode is not mapped back onto the named parameters.
What it costs you: implicit undefined returns, and the fact that a function's behaviour depends on how it is called (this, new, call). Name functions that do work; keep them small enough that the return value and the side effects are obvious. A function that both mutates an object and returns a different object is hard to reason about.
Reach for a function whenever a piece of work has a name, needs reuse, or must be handed to someone else to call later.
Code Examples
function area(width, height) {
return width * height;
}
console.log(area(3, 4)); // => 12
console.log(area(3)); // => NaN (height is undefined)
console.log(area(3, 4, 99)); // => 12 (extra args ignored)
// First-class: pass a function into another function.
function applyTax(price, calc) {
return calc(price);
}
console.log(applyTax(100, (p) => p * 1.1)); // => 110A missing return is undefined, even if the last line looks like a result.
// A missing return is undefined, even if the last line looks like a result.
function brokenDouble(n) {
n * 2;
}
console.log(brokenDouble(5)); // => undefined
function double(n) {
return n * 2;
}
console.log(double(5)); // => 10const ops = {
add(a, b) {
return a + b;
},
};
console.log(ops.add(2, 3)); // => 5
console.log(typeof ops.add); // => functionCommon Gotchas
Forgetting `return` yields `undefined`
a trailing expression is not returned. Arrow functions need parentheses or no braces for the implicit return.
Extra and missing arguments are silent
the engine does not throw on arity mismatch. Defaults and rest parameters make the contract visible.
`arguments` is not an array and is absent in arrows
convert with `[...arguments]` only in `function` functions, or use `...args` instead.
Functions are objects
they can have properties (`fn.cache = new Map()`), and `typeof fn` is `"function"`, a subtype of object.