JavaScript Return Statement (original) (raw)
Last Updated : 07 Feb, 2025
The return statement in JavaScript is used to end the execution of a function and return a value to the caller. It is used to control function behaviour and optimise code execution.
**Syntax
return [expression]
- **Expression Evaluation: The expression inside the brackets is evaluated and returned to the caller.
- **List of Results: When returning multiple values, arrays or objects can be used to structure the data.
How does the Return Statement Work?
The return statement stops function execution and returns a value to the caller. If no value is specified, the function returns undefined by default.
JavaScript `
function add(a, b) { return a + b; }
const res = add(5, 10); console.log(res);
`
In this example, the return statement returns the sum of a and b to the caller.
Using Return Without a Value
If a return statement is used without a value, the function will return undefined.
JavaScript `
function noReturn() { return; }
console.log(noReturn());
`
Returning Value from function
You can return values from a function using the return statement, and these values can be of any type (number, string, object, etc.).
JavaScript `
function greet(name) { return 'Hello, ' + name; } let message = greet('Pranjal'); console.log(message);
`
- The greet function returns a concatenated string 'Hello, ' with the provided name, creating a personalized greeting.
- The returned message is stored in the message variable and logged to the console, displaying the greeting.
Exiting a Function Early
The return statement can be used to exit from a function early when a condition is met, skipping the remaining code.
JavaScript `
function checkE(number) { if (number % 2 !== 0) { return false; } return true; } console.log(checkE(4)); console.log(checkE(5));
`
- The checkE function returns false if the number is odd.
- It returns true if the number is even.
Returning Objects, Arrays, and Functions
You can return complex data structures, such as objects, arrays, or even other functions.
JavaScript `
function Person(name, age) { return { name: name, age: age }; } let person = Person('Ishank', 30); console.log(person);
`
Output
{ name: 'Ishank', age: 30 }
- The createPerson function returns an object containing the name and age properties initialized with the given arguments.
- The object returned by the function is stored in the person variable, and when logged, it displays the person's details: { name: 'Ishank', age: 30 }.
Returning from Arrow Functions
Arrow functions simplify returning values. If there is only one expression, the return keyword and curly braces can be omitted.
JavaScript `
const add = (a, b) => a + b; console.log(add(3, 4));
`
- The add function is an arrow function that returns the sum of two parameters, a and b.
- It is called with the arguments 3 and 4, and the result 7 is logged to the console.
Returning in Recursion
The return statement is essential in recursive functions to pass values back at each step of the recursion.
JavaScript `
function factorial(n) { if (n === 0) { return 1; } return n * factorial(n - 1); } console.log(factorial(5));
`
- The factorial function calculates the factorial of a number n recursively, returning 1 when n is 0.
- For the input 5, it recursively multiplies 5 * 4 * 3 * 2 * 1, returning the result 120.
No return vs return undefined
If a function doesn't include a return statement, it returns undefined by default. However, explicitly using return undefined returns the value undefined.
JavaScript `
function noRet() { } console.log(noRet());
JavaScript
function retUn() { return undefined; } console.log(retUn());
`
- The noRet function does not have a return statement, so it implicitly returns undefined.
- When called, undefined is logged to the console as the output.
Return Value Can Be Used in Expressions
The value returned from a function can be directly used in other expressions or operations.
JavaScript `
function square(x) { return x * x; } console.log(square(2) + 10);
`
- The square function returns the square of the input x by multiplying x with itself.
- When square(2) is called, it returns 4, and adding 10 gives the result 14, which is logged to the console.
Best Practices for Using Return Statement
- **Always return a value when needed: Avoid implicit undefined returns unless intentional.
- **Use return early: Exit functions as soon as possible to improve readability.
- **Return structured data: Use objects or arrays to return multiple values efficiently.
- **Avoid returning inside loops unnecessarily: Optimize performance by minimizing unnecessary function exits.