Core JavaScript Reference 1.5: 2 Top-Level Properties and Functions

This chapter contains all JavaScript properties and functions not associated with any object. In the ECMA specification, these properties and functions are referred to as properties and methods of the global object.

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

Core function

Implemented in

JavaScript 1.5, NES 6.0

ECMA version

ECMA-262 Edition 3.

SyntaxencodeURIComponent(uri)

Parameters

uri

A component of a Uniform Resource Identifier.

DescriptionAssumes that the URI is a URI component rather than a complete URI, so does not treat reserved characters as if they have special meaning and encodes them. See encodeURI for the list of reserved characters.

encodeURIComponent replaces all characters except the following with the appropriate UTF-8 escape sequences:

Evaluates a string of JavaScript code without reference to a particular object.

Core function

Implemented in

JavaScript 1.0

JavaScript 1.4: eval cannot be called indirectly

ECMA version

ECMA-262

Syntaxeval(string)

Parameters

string

A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Descriptioneval is a top-level function and is not associated with any object.

The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.

If the argument of eval is not a string, eval returns the argument unchanged. In the following example, the String constructor is specified, and eval returns a String object rather than evaluating the string.

Example 2. In the following example, the getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.

Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.

JavaScript 1.3 (In previous versions, Infinity was defined only as a property of the Number object.)

ECMA version

ECMA-262

SyntaxInfinity

DescriptionInfinity is a top-level property and is not associated with any object.

The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number including itself. This value behaves mathematically like infinity; for example, anything multiplied by Infinity is Infinity, and anything divided by Infinity is 0.

DescriptionisFinite is a top-level function and is not associated with any object.

You can use this method to determine whether a number is a finite number. The isFinite method examines the number in its argument. If the argument is NaN, positive infinity or negative infinity, this method returns false, otherwise it returns true.

ExamplesYou can check a client input to determine whether it is a finite number.

JavaScript 1.0: If the first character of the string specified in parseFloat(string) cannot be converted to a number, returns NaN on Solaris and Irix and 0 on all other platforms.

JavaScript 1.1, NES 2.0: Returns NaN on all platforms if the first character of the string specified in parseFloat(string) cannot be converted to a number.

ECMA version

ECMA-262

SyntaxparseFloat(string)

Parameters

string

A string that represents the value you want to parse.

DescriptionparseFloat is a top-level function and is not associated with any object.

parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

If the first character cannot be converted to a number, parseFloat returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

Parses a string argument and returns an integer of the specified radix or base.

Core function

Implemented in

JavaScript 1.0: If the first character of the string specified in parseInt(string) cannot be converted to a number, returns NaN on Solaris and Irix and 0 on all other platforms.

JavaScript 1.1, LiveWire 2.0: Returns NaN on all platforms if the first character of the string specified in parseInt(string) cannot be converted to a number.

ECMA version

ECMA-262

SyntaxparseInt(string[, radix])

Parameters

string

A string that represents the value you want to parse.

radix

An integer that represents the radix of the return value.

DescriptionparseInt is a top-level function and is not associated with any object.

The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

If the radix is not specified or is specified as 0, JavaScript assumes the following:

If the input string begins with "0x", the radix is 16 (hexadecimal).

If the input string begins with "0", the radix is eight (octal). This feature is deprecated.

If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.