HOLLA TECH - LEARN

Function Return
 

A function can have an optional return statement. It is used to return a value from the function.

This statement is useful when making calculations that require a result.  

NOTE!
When JavaScript reaches a return statement, the function stops executing.

Function Return
 

Use the return statement to return a value.

For example, let’s calculate the product of two numbers, and return the result.

function myFunction(a, b) {
   return a * b;
}

var x = myFunction(5, 6);
// Return value will end up in x
// x equals 30 

]/su_quote] 

NOTE!
If you do not return anything from a function, it will return undefined.

Function Return
 

Another example:

function addNumbers(a, b) {
   var c = a+b;
   return c;
}
document.write( addNumbers(40, 2) );
//Outputs 42 

 

NOTE!
The document.write command outputs the value returned by the function, which is the sum of the two parameters.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE JAVASCRIPT MAIN COURSE PAGE. 

JAVASCRIPT MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT