JavaScript Predefined Functions

This page discusses JavaScript predefined functions such as escape, unescape, eval, isNaN, tostring, alert, and others.

Conversion and Comparison

  • escape(string) - Encodes a string from ASCII into an ISO Latin-1 (ISO 8859-1) character set which is suitable for HTML processing. It is passed the string to be encoded, and returns the encoded string.
  • unescape - Converts an ISO8859-1 character set to ASCII.

    If a string is "My phone # is 123-456-7890", after the escape function, it is "My%20phone%20%23%20is%20123-456-7890". After unescape, it is "My phone # is 123-456-7890".

  • eval()- Converts a string to integer or float value. It can also evaluate expressions included with a string. In the example, value1 becomes 62.

    value1 = eval("124/2") ,

  • isNaN(value) - If the value passed is a not a number, the boolean value of true is returned, if it is a number, it returns false.
    if (isNaN(string1))
    {
       document.write("String1 is not a number\n");
    }
    else
    {
       document.write(String1 is a number\n");
    }
    
  • parseFloat() - Returns floating point numbers the same as the parseInt function, but looks for floating point qualified strings and returns their value as a float. The first line below returns a value of 123, and the second line returns 9.683.
    value1 = parseFloat("123")
    value2 = parseFloat("9.683")
    
  • parseInt()- Converts a string to an integer returning the first integer encountered which is contained in the string. In the example, value1 becomes 12.

    value1 = parseInt("12b13") ,

    If no integer value were found such as in the string "abcd", then a value of 0 is returned.

  • typeof operator - This is an operator but its usefullness is similar to a function. This operator returns the type of the object it operates on. Values returned are string values and may be one of "undefined", "object", "function", "number", "Boolean", or "string". The example will return the string "number".

    typeof 10

  • taint
  • untaint(element) -
  • valueOf()



Methods that belong to all objects

  • toString() - Converts an object to a string. The syntax is shown below. Radix is the base value for the conversion, which may be 10 for decimal conversion. The value 1 is used for binary conversion, and 8 is used for octal conversion.

    object.tostring(radix)

Dialog Boxes

  • alert(message) - Displays an alert box with a message defined by the string message. Example:

    alert("An error occurred!")

  • confirm(message) - When called, it will display the message and two boxes. One box is "OK" and the other is "Cancel". If OK is selected, a value of true is returned, otherwise a value of false is returned. An example:
    if (confirm("Select OK to continue, Cancel to abort"))
    {
       document.write("Continuing")
    }
    else
    {
       document.write("Operation Canceled")
    }
    
  • prompt(message) - Displays a box with the message passed to the function displayed. The user can then enter text in the prompt field, and choose OK or Cancel. If the user chooses Cancel, a NULL value is returned. If the user chooses OK, the string value entered in the field is returned. An example:
    var response=prompt("Enter your name")
    if (response != null)
    {
       document.write(response)
    }