What does the return function do in javascript?

AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.



The return function is a fundamental concept in JavaScript that plays a crucial role in programming. It allows a function to return a value to the code that called it, enabling the function to pass data back to the calling code. In this article, we will explore the return function in JavaScript, its syntax, and its various use cases.

The Syntax of the Return Function

In JavaScript, the return statement is used within a function to specify the value that the function should return. The basic syntax of the return function is as follows:

function functionName() {
// code block
return value;

The `return` keyword is followed by the value that the function will return. This value can be of any data type, including numbers, strings, booleans, objects, or even other functions.

Returning a Value from a Function

When a function encounters a `return` statement, it immediately exits, and the value specified after the `return` keyword is returned to the calling code. This allows the calling code to use the returned value for further processing or manipulation.

Let’s consider an example to illustrate this concept:

function addNumbers(a, b) {
return a + b;

var result = addNumbers(5, 3);
console.log(result); // Output: 8

In this example, the `addNumbers` function takes two parameters, `a` and `b`, and returns their sum. The `result` variable stores the returned value of the function call `addNumbers(5, 3)`, which is then printed to the console. In this case, the output will be `8`.

Using the Return Value

The returned value can be used in various ways within the calling code. It can be assigned to a variable, used in mathematical calculations, passed as an argument to another function, or used in conditional statements.

Let’s explore a few examples to understand how the return value can be utilized:

Example 1: Assigning the return value to a variable

function multiplyNumbers(a, b) {
return a * b;

var product = multiplyNumbers(4, 6);
console.log(product); // Output: 24

In this example, the return value of the `multiplyNumbers` function, which is the product of `4` and `6`, is assigned to the `product` variable. The value of `product` is then printed to the console, resulting in `24`.

Example 2: Using the return value in conditional statements

function isEven(number) {
if (number % 2 === 0) {
return true;
} else {
return false;

var num = 8;
if (isEven(num)) {
console.log(“The number is even.”);
} else {
console.log(“The number is odd.”);

In this example, the `isEven` function checks if a given number is even or odd. If the number is divisible by 2 without a remainder, the function returns `true`; otherwise, it returns `false`. The return value of the function is then used in an `if` statement to determine whether the number is even or odd.


The return function in JavaScript allows functions to pass data back to the calling code. It plays a crucial role in programming by enabling functions to return values that can be used for further processing or manipulation. Understanding how to use the return function is essential for writing effective and reusable JavaScript code.


– developer.mozilla.org
– www.w3schools.com