Open In App

How To Use Return in JavaScript?

Last Updated : 17 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, the return statement is used to specify the value that a function should produce when it is called. It helps in controlling the flow of execution and provides meaningful outputs from functions. Understanding how return works is important for writing efficient and reusable code.

What is the return statement?

The return statement in JavaScript is used to end a function execution and send a value back to where the function was called. It allows the function to provide useful results, like numbers, strings, or objects, that can be used in other parts of the code.

If there’s no value after return, the function will return undefined by default. The return statement also stops the function’s execution, meaning no further code inside the function will run once it’s called.

Syntax

return [value];

'value' is the data you want to return from the function. If no value is specified, the function returns undefined by default

Exiting a Function with a return

When a return statement is executed in a function, the function exits immediately. Any code written after the return statement is ignored.

JavaScript
function g(name) {
    console.log("Hello, " + name);
    return;
    console.log("This will not be executed");
}

g("GFG");

Output
Hello, GFG
  • The function prints "Hello, GFG" to the console.
  • The return statement stops the function, so the second console.log is never executed.

Returning Values from a Function

Functions in JavaScript can return values. This is essential when you need to calculate or process data inside a function and return the result to the caller.

JavaScript
function sum(a, b) {
    return a + b;
}

let result = sum(5, 3);
console.log(result);

Output
8
  • The sum function returns the sum of a and b: When the sum(5, 3) is called, it adds 5 and 3, then returns the result, which is 8.
  • The result is stored and printed: The returned value (8) is stored in the variable result and then logged to the console, so the output will be 8.

Returning Objects from a function

In JavaScript we can also return object from a function this is a special type of behavior of JavaScript as each and every thing in JavaScript is an object.

JavaScript
function create(name, age) {
    return { name: name, age: age };
}
let p = create("Pranjal", 21);
console.log(p);

Output
{ name: 'Pranjal', age: 21 }
  • The create function returns an object with name and age properties.
  • Calling create("Pranjal", 21) stores the object in p, and console.log(p) prints { name: "Pranjal", age: 21 }.

Returning Arrays from a function

In JavaScript we can also return arrays from a function because arrays are also converted into objects at last as Object is the top level hierarchy in JavaScript.

JavaScript
function get() {
    return [1, 2, 3, 4];
}
let numbers = get();
console.log(numbers);

Output
[ 1, 2, 3, 4 ]
  • The get function returns an array [1, 2, 3, 4].
  • The returned array is stored in numbers, and console.log(numbers) prints [1, 2, 3, 4].

Returning Function from a function

In JavaScript, functions are first-class citizens, meaning they can be returned from other functions.

JavaScript
function o() {
    return function i() {
        console.log("Hello from the inner function!");
    };
}
let m = o();
m();

Output
Hello from the inner function!
  • The function o returns another function i, which prints a message.
  • Calling o() assigns the inner function i to m, and then m() calls i to print "Hello from the inner function!".

Implicit Return in Arrow Functions

In JavaScript, arrow functions allow you to return a value automatically without using the return keyword, making the code shorter and easier to write for simple functions.

JavaScript
const add = (a, b) => a + b;
console.log(add(2, 3)); 

Output
5
  • The arrow function add takes two parameters, a and b, and returns their sum.
  • console.log(add(2, 3)) calls the function with arguments 2 and 3, printing the result 5.

return in Recursive Functions

In recursive functions, return plays a vital role in terminating the recursion and passing the result back through the recursive calls.

JavaScript
function facto(n) {
    if (n === 0) {
        return 1;
    } else {
        return n * facto(n - 1);
    }
}
console.log(facto(4));

Output
24
  • The facto function calculates the factorial of n using recursion by calling itself with n - 1 until n === 0.
  • When n reaches 0, the function returns 1, which stops the recursion.
  • For example, facto(4) computes 4 * 3 * 2 * 1, returning 24.
Introduction to Recursion - GeeksforGeeks

No return in Void Functions

In JavaScript, some functions may not return any value explicitly, meaning they return undefined by default. These functions can still perform actions like logging or modifying data.

JavaScript
function log() {
    console.log("This function does not need a return value");
}

log();

Output
This function does not need a return value
  • The log function performs an action (printing a message) but doesn't return any value.
  • It implicitly returns undefined, as no return statement is provided.

Default Return Values

If no return statement is provided in a function, the function returns undefined by default.

JavaScript
function N() { }

let r = N();
console.log(r);

Output
undefined
  • The function N is empty and doesn't contain any code or a return statement.
  • When called, N() returns undefined, which is assigned to the variable r, and then undefined is logged to the console.

Advantages of using return in JavaScript

  • Returns Values: Functions can return a value, letting you use the result elsewhere.
  • Reusability: Return enables functions to be reused with different inputs.
  • Function Chaining: Returned values allow chaining functions for cleaner code.
  • Early Exit: Return can stop a function before completion, saving resources.
  • Readability: It clearly indicates that a function is meant to produce a result.

Conclusion

In summary, the return statement is essential in JavaScript functions, allowing them to send values back to the caller. It improves code reusability, readability, and efficiency by enabling function chaining and early exits. Understanding how to use return effectively helps write cleaner and more flexible code.


Next Article

Similar Reads