Table of contents
1.
Introduction
2.
Description
3.
Syntax
4.
Example
5.
Returning a Single Value
5.1.
Example 1: Returning a Number
5.2.
Example 2: Returning a String
6.
Returning Multiple Values Using an Object
6.1.
Example 1: Using an Object
6.2.
Example 2: Using an Array
7.
No Return and Empty Return Statement
7.1.
Example 1: Function Without a Return Statement
7.2.
Example 2: Empty Return Statement
8.
Browser Support  
9.
Frequently Asked Questions
9.1.
What happens if a function does not have a return statement?
9.2.
Can a JavaScript function return multiple values?
9.3.
What is the purpose of an empty return statement?
10.
Conclusion
Last Updated: Feb 8, 2025
Easy

JavaScript Function Return Value

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript function return value allows a function to send data back to where it was called. This is useful for performing calculations, processing data, and improving code efficiency. When a function executes a return statement, it stops further execution and returns the specified value. If no return is used, the function returns undefined.

JavaScript Function Return Value

In this article, we will discuss how JavaScript functions return values, including returning single and multiple values, and cases where no value is returned. 

Description

A function in JavaScript is like a small machine that performs a specific task. When this machine finishes its job, it can give back a result. This result is called the "return value." The return value is important because it allows the function to communicate with the rest of your program. Without a return value, the function would just do its job & then stop without sharing any information.  

Let’s break it down step by step. A function is created using the `function` keyword. Inside the function, you write the logic or steps that the function needs to perform. If you want the function to share its result, you use the `return` keyword followed by the value you want to send back. Once the function hits the `return` statement, it stops running & gives back the value.  

Let’s take an example of a simple function that adds two numbers & returns the result:  

// Function to add two numbers  @function addNumbers(a, b) {  @    let sum = a + b;  @    return sum;  @}  @@@// Using the function  @let result = addNumbers(5, 7);  @console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

12  


In this code, the `addNumbers` function takes two inputs (`a` & `b`), calculates their sum, & then uses the `return` keyword to send the sum back. Outside the function, we store the returned value in a variable called `result` & then print it using `console.log`.  

It’s also important to note that if a function doesn’t have a `return` statement, it will automatically return `undefined`. For example:  

function sayHello() {  
    console.log("Hello!");  
}  
let greeting = sayHello();  
console.log(greeting); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

undefined  


In this case, the `sayHello` function only prints "Hello!" to the console but doesn’t return anything. That’s why the variable `greeting` ends up being `undefined`.  

Understanding how return values work helps you write functions that are reusable & efficient. You can take the output of one function & pass it as input to another, making your code modular & easier to manage.  

Syntax

The basic syntax of using a return statement in JavaScript is:

function functionName(parameters) {
    // Code execution
    return value; // Returns a value
}
  • functionName: Name of the function.
     
  • parameters: Input values passed to the function.
     
  • return value: The output value the function returns.

Example

function add(a, b) {
    return a + b;
}

let result = add(5, 10);
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

15

 

In this example, the add function returns the sum of two numbers.

Returning a Single Value

A function can return a single value, which can be a number, string, boolean, or any other data type.

Example 1: Returning a Number

function square(num) {
    return num * num;
}
console.log(square(4)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

16

Example 2: Returning a String

function greet(name) {
    return "Hello, " + name + "!";
}
console.log(greet("Alice"));
You can also try this code with Online Javascript Compiler
Run Code


Output: 

Hello, Alice!

Returning Multiple Values Using an Object

JavaScript functions cannot return multiple values directly, but we can use objects or arrays to return multiple values.

Example 1: Using an Object

function getUser() {
    return {
        name: "John",
        age: 25,
        country: "USA"
    };
}

let user = getUser();
console.log(user.name); 
console.log(user.age);  
console.log(user.country);
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

John
25
USA

Example 2: Using an Array

function getCoordinates() {
    return [10.5, 20.3];
}

let coordinates = getCoordinates();
console.log(coordinates[0]); 
console.log(coordinates[1]); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

10.5
20.3

 

Using objects or arrays allows us to return multiple values efficiently.

No Return and Empty Return Statement

If a function does not explicitly return a value, it returns undefined by default.

Example 1: Function Without a Return Statement

function sayHello() {
    console.log("Hello!");
}

let result = sayHello();
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code

Output: 

undefined

 

Since there is no return statement, the function returns undefined.

Example 2: Empty Return Statement

An empty return statement without a value also returns undefined.

function testFunction() {
    return;
}
console.log(testFunction());
You can also try this code with Online Javascript Compiler
Run Code


Output: 

undefined

 

This is useful when we want to exit a function early without returning any value.

Browser Support  

JavaScript functions & their return values are supported by all modern web browsers. This includes popular ones like Google Chrome, Mozilla Firefox, Microsoft Edge, & Safari. Since JavaScript is a core technology for building websites, browser developers ensure that it works consistently across platforms. This means you don’t need to worry about compatibility issues when using functions & return values in your code.  

However, there are a few things to keep in mind. Older browsers, like Internet Explorer, might not support some advanced JavaScript features. But for basic functions & return values, you’re safe with almost any browser. To check if your code works as expected, you can use the browser’s developer tools. These tools allow you to test your JavaScript code & see if the return values are being handled correctly.  

Let’s take an example of how you can test a function with return values directly in the browser console:  

1. Open your browser & press `Ctrl + Shift + J` (Windows) or `Cmd + Option + J` (Mac) to open the developer console.  
 

2. Copy & paste the following code into the console:  

// Function to calculate the area of a rectangle  
function calculateArea(length, width) {  
    let area = length  width;  
    return area;  
}  
// Testing the function  
let rectangleArea = calculateArea(10, 5);  
console.log("The area of the rectangle is:", rectangleArea); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

The area of the rectangle is: 50  


3. Press `Enter` to run the code. You should see the output in the console.  

This simple test confirms that the function works as expected. If you’re working on a project, you can also include error handling to make sure your functions behave properly even if something goes wrong. For example:  

function divideNumbers(a, b) {  
    if (b === 0) {  
        return "Error: Division by zero is not allowed.";  
    }  
    return a / b;  
}  
let result = divideNumbers(10, 0);  
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

Error: Division by zero is not allowed.  


In this example, the function checks if the second number (`b`) is zero before performing the division. If it is zero, the function returns an error message instead of trying to divide by zero, which would cause a problem.  

Frequently Asked Questions

What happens if a function does not have a return statement?

If a function does not have a return statement, it automatically returns undefined.

Can a JavaScript function return multiple values?

A JavaScript function cannot return multiple values directly, but we can use an object or an array to achieve this.

What is the purpose of an empty return statement?

An empty return statement stops function execution and returns undefined. It is useful for terminating a function early.

Conclusion

In this article, we learned how JavaScript functions return values and why they are important. We discussed the return statement, how to return multiple values, and the best ways to use it. Understanding function return values helps in writing clean and reusable code. This makes JavaScript programs more efficient and easier to manage.

Live masterclass