Loading

Quipoin Menu

Learn • Practice • Grow

In JavaScript, functions are often used to perform calculations, process data, or generate results.
To send a result back from a function, JavaScript provides the return statement.

The return statement is one of the most important concepts to understand when working with functions.


What Is the Return Statement?

The return statement is used to:

  • Send a value back from a function
  • End the execution of a function
  • Make functions reusable and meaningful
Once a return statement is executed, the function stops running immediately.


Syntax

return expression;

  • expression is the value you want to return
  • This value can be a number, string, boolean, object, or any expression


Example

function add(a, b) {
    return a + b;
}

let result = add(10, 5);
console.log(result);


Output

15

Here:

  • a + b is returned
  • The returned value is stored in result


Difference Between Return and console.log( )

This is a very common confusion.


Usign console.log( )

function multiply(a, b) {
    console.log(a * b);
}

  • Displays output
  • Does not give the value back to the caller


Using return

function multiply(a, b) {
    return a * b;
}

  • Returns the value
  • Can be reused, stored, or passed to another function

Rule:
Use console.log() to display.
Use return to send data back.


Returning Different Data Types

Returning a Number

function square(num) {
    return num * num;
}


Returning a String

function greet(name) {
    return "Hello " + name;
}


Returning a Boolean

function isAdult(age) {
    return age >= 18;
}


Returning Multiple Values (Using Array or Object)

JavaScript functions can return only one value, but that value can be an array or object.


Using an Array

function getNumbers() {
    return [10, 20, 30];
}


Using an Object

function getUser() {
    return {
        name: "Rahul",
        age: 21
    };
}


Return Statement Ends Function Execution

Any code written after return will not execute.

function test() {
    return "Done";
    console.log("This will not run");
}

This line is unreachabel.


Function Without Return Statement

If a function does not use return, it returns undefined by default.

function showMessage() {
    console.log("Hello");
}

let output = showMessage();
console.log(output);


Output

Hello
undefined


Conditional Return

A function can return different values based on conditions.

function checkNumber(num) {
    if (num > 0) {
        return "Positive";
    }
    return "Negative or Zero";
}


Why the Return Statement Is Important

  • Makes functions reusable
  • Helps in calculations and logic building
  • Allows chaining and nesting of functions
  • Essential for real-world applications
  • Required for APIs, validations, and data processing
Without return, functions lose their real power.


Real World Example

function calculateTotal(price, quantity) {
    return price * quantity;
}

let totalAmount = calculateTotal(500, 3);
console.log("Total:", totalAmount);


Output

Total: 1500

This pattern is widely used in:

  • Shopping carts
  • Forms
  • Billing systems
  • Calculations


Two Minute Drill

  • return sends a value back from a function
  • It immediately stops function execution
  • A function whitout return gives undefined
  • return is different from console.log()
  • Functions can return numbers, strings, booleans, arrays or objects
  • Code after return never runs