Open In App

What is the use of the return Keyword in JavaScript Functions ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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;

Example: Here, the add function takes two parameters a and b, and returns their sum using the return keyword. When add(3, 5) is called, the function calculates the sum of 3 and 5, and returns 8, which is then stored in the result variable. Finally, console.log(result) prints 8 to the console.

Javascript




function add(a, b) {
    return a + b;
}
 
let result = add(3, 5);
console.log(result); // Output: 8


Output

8

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads