Javascript Functions

Functions Returning Data

Functions may or may not return data.  The return
statement terminates the function call and returns
control of the program to the block of code from where
the function was called.

A return statement may include data in form of a variable 
or a literal.  

Note: Javascript does not include a native trim( ) function 
that strips whitespace from the front or end of a string. 
This has to be built in into any validation (perhaps best with 
regular expressions). If this function receives a string of spaces, 
Javascript would translate it into 0 and hence consider it a number!

The result:

Enter test:

Return:

The code:

<script type="text/javascript">
<!--
  
function isNumber(x)
{
    // multiply x by 1;
    // if x contains a string character, Javascript will
    // return an error message, NaN ("not a number").
    var y = x * 1;

    // This message must be turned into a string, however,
    // before it can be tested.
    // Also, check if there is anything in x; if it were
    // an empty string, "", its value would be 0 and
    // thus a number
    
    if (y.toString() == "NaN" || x == "")
    {
      return false;
    }
    else
    {
      return true;
    }
}

// -->
</script>