Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript return Statement

JavaScript return Statement

The JavaScript return statement is used to return a particular value from the function. The function will stop the execution when the return statement is called and return a specific value. The return statement should be the last statement of the function because the code after the return statement won’t be accessible.

We can return any value i.e. Primitive value (Boolean, number and string, etc) or object type value ( function, object, array, etc) by using the return statement.

Syntax:

return value;

 Where:

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

Example 1: It is the basic example of using a return statement.

Javascript




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


Output

60

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

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments