Open In App

JavaScript return Statement

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The return statement in JavaScript is used to end the execution of a function and specifies the value to be returned by that function.

It can be used to return a specific value, variable, or expression to the caller. If no value is provided, the function returns undefined by default.

Syntax:

return value;
  • value: The value returned to the function caller. It is an optional parameter. If the value is not specified, the function returns undefined

JavaScript return Statement Examples

Example 1: This code defines a function `Product(a, b)` that takes two parameters `a` and `b`. It returns the product of `a` and `b` by multiplying them. Then, it calls the `Product()` function with arguments `6` and `10` and logs the result, which is `60`.

Javascript
function Product(a, b) {
    // Return the product of a and b
    return a * b;
};
console.log(Product(6, 10));

Output
60

Example 2: Here, we will return multiple values by using the object and we are using ES6 object destructuring to unpack the value of the object.

Here, the code defines a function `Language()` that returns an object containing three properties: `first`, `second`, and `Third`, each storing a string value. Then, it uses object destructuring to assign these properties to variables `first`, `second`, and `Third`. Finally, it logs the values of these variables.

Javascript
function Language() {
    let first = 'HTML',
        second = 'CSS',
        Third = 'Javascript'
    return {
        first,
        second,
        Third
    };
}
let { first, second, Third } = Language();
console.log(first);
console.log(second);
console.log(Third);  

Output
HTML
CSS
Javascript



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads