Team LiB
Previous Section Next Section

Invoking Functions

The most important thing to know about using functions is how to make them work. Making a call to or invoking a function can be done anywhere within a Web page. Only three conditions need to be met for a function call to succeed. First, the function must have been previously defined in the program. Second, the correct number of parameters must be passed to it. Lastly, the correct object must be present—you cannot call the string object's split function without a string object. (For some functions, the last stipulation does not apply, as you will soon see.)

Use the following general syntax to invoke functions:

<function name>( <parameter list> );

When invoking a function, always follow the function name with a set of parentheses containing an optional parameter list. If the function returns a value, it can be set as the value of a variable, like this:

var binaryString = toBinary( 789 );

This statement (using the aforementioned toBinary() function) will cause the variable binaryString to have the value "1100010101".

JavaScript Top-Level Functions

There are many top-level functions—also known as global functions —that can be used anywhere in a JavaScript Program. The following subsections describe those functions.

escape(string)

The top-level function escape() encodes a string parameter, so that it may be portable. A string is considered portable if can be transmitted across any network to any computer that recognizes ASCII characters.

In the encoding process, all characters with ASCII values larger than 69 are converted to their hexadecimal escape sequence.

Following is an example of the escape():

var myString = "!#$%^&";

document.write( escape( myString ) );

The output from this code segment would be

  • %21%23%24%25%5E%26.

eval(string)

The top-level function eval() evaluates, and, if possible, executes a string containing JavaScript code that is passed as a parameter. The eval function can also be used to evaluate mathematical expressions.

Following is an example of the eval() function:

document.write( eval( "( 9 + 5 ) * 2" ) );

The output from this code segment would be

  • 28.

isFinite(number)

The top-level function isFinite() determines whether a number is finite and, therefore, a legal number. If the parameter is a legal number, the function returns true; otherwise, it returns false.

Following is an example of the isFinite() function:

document.write( isFinite( 1001 ) );

The output from this code segment would be

  • True

isNaN(string)

The top-level function isNaN() determines whether the string passed as a parameter is not a number. If the string is not a number, the function returns true; otherwise it returns false.

Following is an example of the isNaN() function:

document.write( isNaN( "Hello World!!" ) + ", " );

document.write( isNaN( 42 ) );

The output from this code segment would be true, false.

number(object)

The top-level function number() attempts to convert an object to its numerical representation. If the function succeeds, the numerical value is returned; otherwise, the value NaN is returned.

Following is an example of the number() function:

document.write( number( "Hello World!!" ) + ", " );

document.write( isNaN( "42" ) );

The output from this code segment would be

  • NaN, 42 (the number value).

parseFloat(string)

The top-level function parseFloat() attempts to convert a string parameter into a floatingpoint number. The number must start at the beginning of the string for the function to succeed. Any non-numeric characters following the number are discarded. If the function succeeds, the floating-point number is returned; otherwise the value NaN is returned.

Following is an example of the parseFloat() function:

document.write( parseFloat( "3.14" ) + ", " );

document.write( parseFloat( "3.14string" ) + ", " );

document.write( isNaN( "string" ) );

The output from this code segment would be

  • 3.14, 3.14, NaN.

parseInt(string, radix)

The top-level function parseInt() attempts to convert a string parameter into an integer number. The number must start at the beginning of the string for the function to succeed. Any non-numeric characters following the number are discarded. If the function succeeds, the integer number is returned; otherwise, the value NaN is returned.

An optional parameter radix allows you to specify the number base from which to convert the number. The parameter may be anything from 2 to 36. A radix of 10 (decimal) is the default value.

Following is an example of the parseInt() function:

document.write( parseInt( "42" ) + ", " );

document.write( parseInt( "42string" ) + ", " );

document.write( isNaN( "string" ) );

The output from this code segment would be

42, 42, NaN.

string(object)

The top-level function string() converts an object parameter into its string representation.

Following is an example of the string() function:

var myBool = new Boolean( 0 );

document.write( string( myBool ) );

The output from this code segment would be false.

taint()

The top-level function taint() is deprecated. This means that it is still technically part of the JavaScript language, but should no longer be used.

unescape()

The top-level function unescape() performs the opposite operation from that of the top-level function escape. The unescape function searches for hexadecimal escape sequences and replaces them with their character equivalents.

Following is an example of the unescape() function:

var myString = "%21%23%24%25%5E%26";

document.write( unscape( myString ) );

The output from this code segment would be

  • !#$%&.

untaint()

The top-level function untaint(0 is deprecated.

Tip 

Remember that JavaScript's top-level functions do not need to be defined anywhere in your program in order for you to use them.


Team LiB
Previous Section Next Section