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

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

Contact Us