4 Tips for Properly Using the Return Statement in JavaScript
The return statement doesn't get much attention, but it's easy to get subtly wrong. It specifies the value a function hands back to its caller and marks the end of that function's execution. Get it wrong and your code can still run without erroring, just not the way you expect. Here are four things worth knowing.
1. Use return to end a function or method
Return signals the end of a function. The moment it's hit, the function stops:
function sayMyName(name) {
console.log(name);
return; // ends the function, returns undefined
}
Worth knowing: return is affected by automatic semicolon insertion (ASI). You can't put the value on the next line and expect it to come along:
// Wrong. ASI inserts a semicolon after return, so this returns
// undefined and never touches `name`.
return
name;
// Right. The parenthesis stays open across the line break, so
// ASI doesn't insert anything.
return (
name
);
2. Return the correct data type
Return the type your caller actually expects. Get this wrong and your program will still run, just not correctly, thanks to JavaScript's type coercion (opens in a new tab) quietly patching over the mismatch until it eventually causes a real bug somewhere downstream.
function add(a, b) {
return a + b; // correct, returns a number
}
function subtract(a, b) {
return a - b; // correct, returns a number
}
function multiply(a, b) {
return `${a} times ${b} is equal to: ${a * b}`; // wrong, returns a string
}
3. Return a value that's appropriate for the function
Think about what your function's name actually promises, and return that, not just whatever happens to be in scope. If a function is meant to calculate an average, returning the sum isn't correct, even though the sum is right there.
function average(numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total; // wrong, this is the sum, not the average
}
function average(numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total / numbers.length; // correct
}
4. Use multiple return statements if it helps
Sometimes you want to return immediately once a condition is met, whether that's inside an if statement or a switch.
function options(option) {
switch (option) {
case "DnD":
return "do-not-disturb";
case "Silent":
return "silent-mode";
default:
return "no-option-selected";
}
}
function getOptions(option) {
if (option === "DnD") {
return "do-not-disturb";
}
return option;
}
In options, returning directly inside each case means no break keyword and no temporary variable to hold the value. It reads better and uses less memory.
In getOptions, you always get a value back regardless of which branch runs: the return inside the if fires when the condition is true, and otherwise the function falls through to the final return option.
None of this is complicated, but getting it wrong tends to produce bugs that only show up later, once something downstream trips over a value that isn't what it expected. Read more about the return statement (opens in a new tab) on MDN.