Javascript Return values
Using the return keyword and the value to be returned, a function in JavaScript can return a value. Any data type, including primitive and reference types, may make up the returned value.
Syntax:
function functionName(parameter1, parameter2, ...) {
// function body
return value;
}
The return statement is used to return a value from the function. If no value is specified in the return statement, the function returns undefined by default.
Example:
function calculateArea(width, height) {
let area = width * height;
return area;
}
let result = calculateArea(5, 10);
console.log(result);
Output:
50
In this example, the calculateArea function takes two parameters (width and height) and calculates the area of a rectangle. The calculated area is returned using the return statement. The result variable stores the returned value, which is then printed to the console using console.log().