Built-In Functions

You have now had your first exposure to the built-in String, Math, and Date Objects. Some of these Objects are more built-in than others. While Date acts like an actual Object, with the exception of its two static methods, the String object is almost invisible. All normal JavaScript programs manipulate strings as if they are a separate data type. The essence of a string is part of the JavaScript language.

There is also a small set of Built-in Functions to JavaScript itself. They are not methods, and are never applied to an instance using the dot operator (.). They are on the same plane as functions that you create using the function keyword. At present, there are 5 such Built-in Functions; they are as follows:

escape() & unescape()

The purpose of the escape() encoding is to ensure that the string is portable to all computers and transmittable across all networks, regardless of the character encoding the computer or networks support (as long as they support ASCII).

The escape() and unescape() functions are used to convert to and from the escape code convention used by HTML. In HTML a number of special characters, such as the HTML delimiters < and >, must be represented in a special way to include them in ordinary text. For example, if you have written any HTML at all then you know that you sometimes need to write %20 to represent a space character. The escape() Built-in Function takes a string representing one of these special characters and returns its escape code in the form %xx, where xx is a two-digit Hexadecimal number.

escape(" ") -- returns %20 -- the code for a space character.
unescape("%20")  -- returns the string " " -- a single space character.

eval()

The built-in function eval() takes a string as its argument. The string can be is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

If the argument represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() performs the statements. All the normal rules for evaluating expressions, including variable substitution, are performed by the eval function. This function is extremely powerful simply because it evaluates any JavaScript expression, no matter what that expression does. You will see a lot more of this function in several subsequent lessons. For the moment, we briefly look at a simple example in which we ask eval() to do some arithmetic for us. If x is a var with the value of 10 then the following two expressions assign 146 to both y and z:

y = ( x * 14 ) - ( x / 2 ) + 11;
z = eval("( x * 14 ) - ( x / 2 ) + 11");

This function is useful for evaluating a string representing an arithmetic expression. For example, input from a form element is always a string, but you often want to convert it to a numerical value.

The following example takes input in a text field, applies the eval function and displays the result in another text field. If you type a numerical expression in the first field, and click on the button, the expression will be evaluated. For example, enter "(666 * 777) / 3", and click on the button to see the result.

<SCRIPT>

function compute(form) {
     form.result.value =
eval(form.expr.value);
}

</SCRIPT>

<FORM NAME="evalform">
     Enter an expression: <
INPUT TYPE="text" NAME="expr">
     Result: <
INPUT TYPE="text" NAME="result">
     <
INPUT TYPE="button" VALUE="Click Me" onClick="compute(this.form)">
</
FORM>

Enter an expression:
Result:

The eval function is not limited to evaluating numerical expressions, however. Its argument can include Object References or even JavaScript statements. For example, you could define a function called setValue that would take two arguments: and object and a value, as follows:

function setValue(myObj, myValue) {
     eval("document.forms[0]." + myObj + ".value") = myValue;
}

Then, for example, you could call this function to set the value of a form element "text1" as follows:

setValue(text1, 42)

eval() examples will be presented throughout the course that will demonstrate the power of this function.

parseInt() and parseFloat()

These two built-in functions return a numeric value when given a string as an argument.

parseFloat() parses its argument, a string, and attempts to return a floating point number. If it encounters a character other than a sign (+ or -), numeral (0 - 9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, it returns NaN - NOTE: it will ignore blank spaces - this is also true for parseInt().

The parseInt() 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. If the first character cannot be converted to a number in the specified radix, it returns NaN. parseInt() truncates numbers to integer values.

parseFloat("+3.14williamtell5") = 3.14
parseInt(10111, 2) = 23

Note that everything after the first w is ignored, since w cannot possibly be part of a floating-point number. The second value is obtained because 23 in binary (base 2) notation is 10111.