In JavaScript, a function return is a statement that specifies the value that should be returned by a function when it is called. The return statement is used to end the execution of the function and return a value to the caller of the function.
Here is an example of using a return statement in a JavaScript function:
// Define a function that accepts two numbers as parameters
function addNumbers(num1, num2) {
// Calculate the sum of the two numbers
var sum = num1 + num2;
// Return the sum
return sum;
}
// Call the function and pass in two numbers
var result = addNumbers(5, 10);
// Log the result to the console
console.log(result); // 15
In this example, we are defining a function named addNumbers()
that accepts two numbers as parameters. The function calculates the sum of the two numbers and uses a return statement to return the sum to the caller of the function.
We are then calling the addNumbers()
function and passing in two numbers as arguments. The function is executed and the return statement is executed, returning the calculated sum to the caller of the function. The result of the function is assigned to a variable and is logged to the console.
We can also use a return statement without specifying a value to simply end the execution of the function and return nothing.
I hope you find this useful. Please comment if you have any questions.