Skip to content

Function Return Statements 2

Every JavaScript function call produces a value. A function returns whatever its return statement gives, and when no return runs, or return has nothing after it, the call returns undefined. This page covers what that means for the caller, and how to return more than one value.

Both of these functions return undefined:

function noReturn() {}
function emptyReturn() {
return;
}
console.log(noReturn());
console.log(emptyReturn());

This prints:

undefined
undefined

A bare return; is useful for leaving a function early when there is nothing to send back, such as when an input is invalid.

A missing branch returns undefined, not false

Section titled “A missing branch returns undefined, not false”

A function meant to return true or false sometimes returns true in one branch and nothing in the other. The missing branch returns undefined, which is falsy, so an if test still works. A strict comparison with false does not:

function isInStock(stock, item) {
if (stock[item] > 0) {
return true;
}
}
const stock = { pens: 12, rulers: 0 };
const result = isInStock(stock, 'rulers');
console.log(result);
console.log(result === false);
console.log(!result);

This prints:

undefined
false
true

Code that relies on falsiness hides the gap until someone writes === false or stores the result, so make every path return a value. Here the whole body can be return stock[item] > 0;, which always returns a boolean. Truthy and Falsey Values lists the values that count as false in a condition.

A function returns one value, but that value can be an object or an array. Destructuring then unpacks it into separate variables at the call:

function summarise(temperatures) {
return {
low: Math.min(...temperatures),
high: Math.max(...temperatures),
};
}
const { low, high } = summarise([14, 9, 17, 11]);
console.log(low, high);
function splitName(fullName) {
const space = fullName.indexOf(' ');
return [fullName.slice(0, space), fullName.slice(space + 1)];
}
const [first, last] = splitName('Ada Lovelace');
console.log(first);
console.log(last);

This prints:

9 17
Ada
Lovelace

Return an object when the values have names that matter to the caller, and an array when their order is obvious, as with a pair of first and last names.

The returned value can itself be a function. The returned function keeps access to the variables of the function that made it, which is called a closure:

function makeCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const next = makeCounter();
console.log(next());
console.log(next());

This prints:

1
2

The same idea is behind Function Currying and behind modules built with an immediately invoked function expression, where a function runs once and returns an object of methods.

JavaScript inserts a semicolon at the end of a line that holds only return, so a value that starts on the next line is never returned:

function makeUser() {
return
{
name: 'Priya'
}
}
console.log(makeUser());

This prints:

undefined

The braces on the following lines are read as a block containing a label, not as an object, so the code runs without an error. Start the returned value on the same line as return, as in return {, to avoid the problem.

Steve Griffith covers this topic in his video What do JS functions really return? And why?.