Adding return statement to functions

A return statement is used to return a specific value to the main script.

function MyFirstFunction () {
    var FirstText = "I used ";
    var SecondText = "the return statement.";
    var TheResult = FirstText + SecondText;
    return TheResult;
}

In this example the first two variables are assigned string values and the TheResult variable is given the value of the addition of those two strings.

Notes:

  • The return statement is placed as the last line of the function before the closing curly bracket.
  • The return statement ends with a semicolon.
  • Not only variable values can be returned, all of the below return statements are valid.
return "This is the return statement.";
return "This is the " + "return statement";
return 77;
return 77 + 43 + 29;
return true;
return null;
return;

Leave a Reply