[Previous] [Table of Contents] [Next]

# Functions and Objects

JScript supports a few built-in functions and objects. In addition, JScript allows you to create your own functions.

## User-Defined Functions

Functions combine several operations under one name. If the function terminates correctly, it can return a result. JScript supports both user-defined and built-in functions. User-defined functions have the following syntax:

    function test(parameters)
    {
        body with statements
        return [return value];
    }

You declare a function using the keyword function followed by a function name and a parameter list set in parentheses. The statements in the function body must be enclosed in braces. The return statement terminates a function and, optionally, returns a value.

To call a function in a script, you insert the function name and the parameters (set in parentheses). If you want to use the return value of a function, the function call must be on the right side of an assignment statement; if you want to ignore the return value, the function call can appear by itself on a line. The following statement calls the function test and assigns the return value to the variable result:

    result = test();

If the function doesn't require parameters, you must use empty parentheses. You use commas to separate multiple parameters.

## Built-In Functions

JScript has a few built-in functions to handle expressions and special characters and to convert strings and numeric values.

The eval function executes JScript code in a string, which is passed as a parameter, and returns the resulting value, as in value = eval("14+15");. The parseFloat function receives a string parameter and tries to convert it to a floating-point value. If the string parameter contains an illegal character (a character that isn't +, _, 0 to 9, ., or e), the string is converted only from the beginning to the illegal character. If the parameter doesn't contain a number, the NaN value is returned. The function parseInt requires a string as the first parameter. The second parameter must contain the code for the base (10 = decimal, 8 = octal, 16 = hexadecimal, and so on). An illegal string causes the result NaN. The parseInt function always returns an integer value.

> NOTE

> For more details on built-in functions in JScript, see the JScript Language Reference.

## Objects

JScript also supports a few built-in objects for processing strings, executing mathematical operations, and manipulating date and time values. You use the String object if a string is assigned to a variable or a property (as in name = "Born";). The object has several methods for manipulating strings. The Math object offers methods and properties for mathematical operators. For example, value = Math.PI; assigns the property PI to the variable value. The Date object handles date and time values. For example, var Name = new Date(parameters); creates a new date object and today = new Date(); returns the current date.

In JScript, you can handle objects and arrays in the same way. You can also access objects and collections in a similar way. To access a method or property of an object, you use the object name followed by a dot and the name of the property or method. The following statement uses the Echo method of the WScript object to display text in a message box:

    WScript.Echo("Hello");

> NOTE

> In JScript, you must submit parameters in parentheses (unlike in VBScript).

To access an object in a collection, you can use an index value. The following statements are equivalent:

    Res = Object.width;
    Res = Object[3];   // [3] should be equivalent to index "width".
    Res = Object["width"];

The brackets are valid when you access the numeric index, but you must omit the dot if an index value is used. Thus, the following statement causes a syntax error:

    Res = Object.3;

If an object contains another object as a property, the naming scheme must be extended as follows:

    var x4 = toDoToday.shoppingList[3].substring(0, 1);

The object property is followed by a dot, which is followed by a subobject.

## Arrays

Arrays are variables that contain a collection of values. When you create and handle arrays in JScript, you must use the Array object, as in the following statement:

    var cities = new Array (10);

This statement creates the variable cities, which can contain 10 array items. The array items use the indexes from 0 to 9. You can access the items in this array using the array name and an index, as shown here:

    cities[0] = "Rome";
    cities[1] = "New York";
    cities[2] = "Detroit";
    cities[3] = "Manila";

The number in brackets is the index value, which indicates an array item. You can estimate the number of array items using the following code:

    number = name.length;

To declare an array and initialize all array elements, you can use the following statement:

    var cdays = new Array("Sunday", "Monday", "Tuesday",
                          "Wednesday", "Thursday", "Friday", "Saturday");

This statement creates a new array and assigns the data in parentheses to the array elements.

> NOTE

> I'll discuss additional JScript features in later chapters. Different JScript language engines are available. Microsoft Windows 98 and Internet Explorer 4 include version 3 of the JScript language engine. Internet Explorer 5 comes with version 5 of the JScript language engine. WSH 2 and Internet Explorer 5.1 use version 5.1.