How To Use Return in JavaScript?
Last Updated :
17 Feb, 2025
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");
- 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);
- 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);
- 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();
OutputHello 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));
- 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));
- 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.

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();
OutputThis 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);
- 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.
Similar Reads
How to Convert Char to String in JavaScript ?
In the article, we are going to learn about conversion Char to a string by using JavaScript, Converting a character to a string in JavaScript involves treating the character as a string, a character is a single symbol or letter, while a string is a sequence of characters. Strings can contain multipl
3 min read
Applications of Recursion in JavaScript
Recursion is a programming technique in which a function calls itself directly or indirectly. Using a recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. Recursion is a t
7 min read
JavaScript Program to Print 1 to N using Recursion
In this article, we will see how to print 1 to N using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
2 min read
JavaScript Program to Print N to 1 using Recursion
In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
1 min read
JavaScript Program to Subtract Two Numbers
In this article, we will see how to subtract two numbers in JavaScript. A Basic arithmetic operation known as Subtraction involves taking one number (the "subtrahend") away from another (the "minuend") in order to calculate their difference. It is denoted by the minus sign (-). A subtraction's outco
3 min read
JavaScript Program to Perform Simple Mathematical Calculation
In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 :
4 min read
JavaScript Program for Multiplication of Two Numbers
In this article, we are going to learn how we can multiply two numbers using JavaScript. Multiplying the two numbers in JavaScript involves performing arithmetic addition on numeric values, resulting in their sum. JavaScript supports numeric data types. In JavaScript, both floating-point and integer
4 min read
JavaScript - Access Elements in JS Array
These are the following ways to Access Elements in an Array:1. Using Square Bracket NotationWe can access elements in an array by using their index, where the index starts from 0 for the first element. We can access using the bracket notation.JavaScriptconst a = [10, 20, 30, 40, 50]; const v = a[3];
2 min read
How to use goto in Javascript ?
There is no goto keyword in javascript. The reason being it provides a way to branch in an arbitrary and unstructured manner. This might make a goto statement hard to understand and maintain. But there are still other ways to get the desired result. The method for getting the goto result in JavaScri
3 min read
When and why to 'return false' in JavaScript?
Return statements, in programming languages, are used to skip the currently executing function and return to the caller function. Return statements may/may not return any value. Below is the example of a return statement in JavaScript. Example 1: javascript // The return statement returns // the pro
2 min read