Back to JavaScript
2026-01-195 min read

return (JavaScript)

Learn return (JavaScript) step by step with clear examples and exercises.

Title: Mastering JavaScript's return Statement: A full guide for Practical Depth

Why This Matters

The return statement is a crucial part of any JavaScript function, allowing you to control the flow of your code and return a value to the caller when necessary. Understanding how to effectively use the return statement can help you write cleaner, more efficient code, and even solve real-world coding challenges that may arise during your programming journey.

Prerequisites

To fully grasp the concepts covered in this guide, you should have a solid understanding of the following topics:

  1. Basic JavaScript syntax and structure
  2. Variables and data types
  3. Control structures (if-else statements, loops)
  4. Functions and function declarations
  5. Understanding the concept of scope

Core Concept

The Basics of return

The return statement is used within a JavaScript function to specify a value that should be returned to the function caller. When a return statement is encountered, the execution of the current function immediately stops, and the specified value or expression is sent back to the calling environment.

function getRectArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
console.log(getRectArea(3, 4)); // Expected output: 12
console.log(getRectArea(-3, 4)); // Expected output: 0

In the example above, we define a function called getRectArea that takes two arguments: width and height. If both parameters are greater than zero, we calculate their product and return it. Otherwise, we return 0. The console.log statements demonstrate how the returned value is passed back to the calling environment (in this case, the global scope).

Early Returns

One of the advantages of using the return statement is that you can stop function execution early if certain conditions are met. This can help improve your code's efficiency by preventing unnecessary computations or operations.

function findSmallest(arr) {
let smallest = arr[0];

for (let i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}

return smallest;
}

const numbers = [3, 5, -2, 8, -7, 1];
console.log(findSmallest(numbers)); // Expected output: -7

In this example, we define a function called findSmallest that takes an array of numbers and returns the smallest number in the array. We initialize a variable called smallest with the first element of the array and then loop through the rest of the elements. If we find a number smaller than our current smallest, we update it. By returning early, we avoid continuing the loop if we've already found the smallest number.

Returning Multiple Values

While JavaScript functions can only return a single value directly, you can use an object or an array to return multiple values as a unit. This approach is often used when you need to return related data that cannot be combined into a single value.

function getCoords(element) {
const rect = element.getBoundingClientRect();
return {
top: rect.top + window.pageYOffset,
left: rect.left + window.pageXOffset
};
}

const myElement = document.getElementById("my-element");
console.log(getCoords(myElement)); // Expected output: Object { top: number, left: number }

In this example, we define a function called getCoords that takes an HTML element and returns its position relative to the viewport as an object with two properties: top and left. We use the getBoundingClientRect() method to get the element's rectangular bounds and then add the current scroll positions (window.pageYOffset and window.pageXOffset) to account for any scrolling that may have occurred.

Worked Example

Finding the Maximum Number in an Array

Let's create a function called findMaxNum that takes an array of numbers and returns the maximum number in the array using the return statement.

function findMaxNum(arr) {
let max = arr[0];

for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

return max;
}

const numbers = [3, 5, -2, 8, -7, 1];
console.log(findMaxNum(numbers)); // Expected output: 8

In this example, we define a function called findMaxNum that takes an array of numbers and returns the maximum number in the array using a loop and the return statement. We initialize a variable called max with the first element of the array and then loop through the rest of the elements. If we find a number greater than our current max, we update it. Finally, we return the maximum value found.

Common Mistakes

  1. Not using parentheses around the expression when returning an object or array: Remember that objects and arrays are expressions in JavaScript, so you should always use parentheses if you're returning one directly from a function.
function getCoords(element) {
const rect = element.getBoundingClientRect();
return {
top: rect.top + window.pageYOffset,
left: rect.left + window.pageXOffset
};
}

// Correct usage
const myElementCoords = getCoords(document.getElementById("my-element"));

// Incorrect usage (without parentheses)
const myElementCoords2 = getCoords(document.getElementById("my-element"));
  1. Returning multiple values without using an object or array: As mentioned earlier, JavaScript functions can only return a single value directly. If you need to return multiple values as a unit, use an object or an array.
function getCoords(element) {
const rect = element.getBoundingClientRect();
return rect.top + window.pageYOffset; // Incorrect usage (only returning one value)
}
  1. Not understanding the difference between return and console.log: The return statement stops the execution of a function and returns a value, while console.log simply outputs a value to the console without affecting the flow of your code.

Practice Questions

  1. Write a JavaScript function called findSum that takes an array of numbers and returns their sum using the return statement.
  2. Modify the getCoords function to also return the width and height of the element's bounding rectangle in addition to its top and left positions.
  3. Write a JavaScript function called findAverage that takes an array of numbers and returns their average using the return statement.

FAQ

  1. What happens when I return from a function without specifying a value?: If you don't specify a value when returning from a function, the default value returned is undefined.
  2. Can I use the return statement inside an if-else block to return different values based on conditions?: Yes, you can use the return statement inside an if-else block to return different values based on the evaluated condition.
  3. What's the difference between return and break in JavaScript?: The return statement stops the execution of a function and returns a value, while the break statement stops the iteration of a loop at the current iteration.
return (JavaScript) | JavaScript | XQA Learn