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 CodeOutput:
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.