Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 09 / 31FUNCTIONS

Functions make promises.

A function has inputs, a result, and assumptions worth naming.

01 / THE BASICS

Start here.

A function gives a reusable block of code a name. function double(value) { return value * 2; } defines one. The word value is a parameter: a local name waiting for an input. double(6) is a call, and 6 is the argument supplied for that call. JavaScript assigns 6 to value, evaluates value * 2, and sends 12 back through return. The call expression double(6) can then be used anywhere a value is allowed.

Defining a function does not run its body. The body runs when the function is called. Every call gets its own parameter values. A return statement ends the current call immediately; instructions after it in that path are not reached. If a function reaches the closing brace with no return, its result is undefined. Displaying a value with console.log is an effect and does not automatically return that value to the caller.

A contract is the promise a function makes to its caller. For double, a simple contract is 'given a number, return twice that number'. The contract helps you decide what to do with unexpected inputs and edge cases. A function that changes an object or writes to the page should say so as well, because its effect matters even if its return value does not. Start by naming the input, result, and any visible change.

02 / THE IDEA

Build the model.

A function packages behavior so callers can rely on a small contract. Parameters receive inputs; return produces a value for the caller. If no return runs, the result is undefined. The most useful function names say what result or effect a caller can expect.

A clear contract includes edge cases: what happens with an empty input, an invalid value, or repeated calls? Pure functions depend only on inputs and have no observable side effects, which makes them easier to reason about. Effects are still necessary; it helps to keep them visible.

03 / UNDER THE SURFACE

How it works.

To define a function, name its parameters and decide what value it returns. Calling it evaluates each argument, binds those values to the parameters, runs the body, and gives the returned value back to the caller. A return ends that call immediately. If the body reaches its end without returning, the caller receives undefined, which can quietly spread into later calculations.

A useful contract says what inputs are accepted and what output follows. For labelFor, the contract might be 'given a whole number of items, return the singular word only for one'. That sentence makes zero and negative values worth discussing. When a function writes to the page or modifies external state, mention that effect too; a caller needs to know more than its return value.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
function labelFor(count) {
  return count === 1 ? 'item' : 'items';
}

First notice. The result depends only on count, so the same input gives the same label.

When labelFor(1) runs, count becomes 1. The comparison is true, so return produces 'item' and the call ends. For labelFor(3), the comparison is false, so it returns 'items'. The function does not remember an earlier count; each call gets its own parameter binding.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
function double(value) {
  return value * 2;
}
console.log(double(6));

It prints 12. A useful contract is 'given a number, return twice that number'. If a caller passes a string, JavaScript may convert it during multiplication; a stronger production contract would validate the input if strings are not intended.

06 / KEEP THIS

One thing to remember.

Explain what a function promises before studying its syntax.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.