Loading, please wait...

Return () Statement

The Return () Statement

The return statement is an optional statement in javascript. This mainly used to return a value from the function.

This statement is beneficial when we want a result while making calculations.

 

Syntax

function  functionName (parameter 1, parameter 2)
{
    Block of code
    return value;
}

 

Code:

<html>
<body>
<h2>The Return Statement</h2>
<script>
function Sum(x,y)
{
   return x+y;
}
total = Sum(2,3);
document.write ("Addition of two number is "+total);
</script>
</body>
</html>

 

In the above example, we have defined a function named Sum() have two parameters x and y.

So, by calling a function if we except addition in return. We must use a return() statement.

Otherwise, it does not return any value, the result will be undefined.

 

Output: