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)); |
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); |
HTML CSS Javascript