The escape() function returns the ASCII encoding of an argument in the ISO Latin-1 character set, useful for writing cookies to the cookies.txt file.
Syntax
The syntax for the escape() function is
escape("string")
where string is any string.
Description
The escape() function is not a method associated with any object but it is part of the JavaScript language. The value returned by escape() is the same string with non-alphanumeric characters encoded in the form "%xx", where xx is the ASCII encoding of a character in the string.
Example
When you run this script
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT Language = "JavaScript">
mystring = "Don't leave yet!"document.write (escape(mystring))
</SCRIPT>
</BODY>
</HTML>the screen displays
Don%27t%20go%20away%21%21
which is "Don't leave yet!" with all the non-alphanumeric characters "escaped" into %xx format.
The eval() function executes a string as through it were a JavaScript statement.
Syntax
The syntax for using the eval function is
eval(string)
where string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
Description
The argument of the eval() function must be a string that can also be executed as a valid JavaScript statement. If the string represents an expression, eval() evaluates the expression. If the string is a numeric sring character, eval() returns the number. If the argument represents one or more JavaScript statements, eval() performs the statements.
Example
When executed, this script
<HTML>
<BODY>
<SCRIPT Language = "JavaScript">
var string1 = "5 * 10"
var string2 = "Hello"
var executable = "document.write('<H3><CENTER>"+string2executable += "</CENTER></H1>')"
document.write ("eval(string1) = ",eval(string1),"<BR>")
document.write ("eval(executable) produces ")
eval(executable)
</SCRIPT>
</BODY>
</HTML>displays this on the screen:
eval(string1) = 50
eval(executable) produces
Hello
On Unix platforms, the isNaN function evaluates an argument to determine if it is NaN (not a number).
Syntax
The syntax for this function is
isNaN(testValue)
where testValue is the value you want to evaluate.
Description
isNaN is available on Unix platforms only. To convert a non-numeric value to a number 0 in the Windows platform, use the custom strToZero() function shown here:
function strToZero(anyval) {
anyval = ""+anyval
if (anyval.substring(0,1) < "0" || anyval.substring(0,1) > "9") {
anyval = "0"
}
return eval(anyval)
}Once this function is defined in the <HEAD> of a Web page, you can use the syntax strToZero(anyvalue) to ensure that anyvalue is a number. (See function Statement for an example.)
The parseFloat() function converts a string containing numeric characters to a floating point number.
Syntax
The syntax for this function is
parseFloat(string)
where string is a string that represents the value you want to parse.
Description
parseFloat() parses its argument, a string, and returns a floating point number. If it finds 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, parseFloat returns 0 (zero) on Windows platforms and NaN the other platforms, indicating that the value is not a number.
Example
When this Web page is opened
<HTML>
<BODY>
<SCRIPT Language = "JavaScript">
string1 = "123 Oak Tree Lane"
string2 = "P.O. Box 630"
document.write ('parseFloat(string1) = ',parseFloat(string1))
document.write ('<BR>')
document.write ('parseFloat(string2) = ',parseFloat(string2))
</SCRIPT>
</BODY>
</HTML>This displays the following on the screen:
The parseInt() function parses a string and returns an integer of the specified radix or base.
Syntax
The syntax for the function is
parseInt(string [,radix])
where string is a string that represents the value that you want to parse and radix is an integer that represents the radix of the return value.
Description
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). A radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes greater than 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 all characters from that point on. Floating point numbers are truncated to integer values. 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 8 (octal).
If the input string begins with any other value, the radix is 10 (decimal).
If the first character of the string cannot be converted to a number, parseInt returns one of the following values:
0 on Windows platforms.
NaN on any other platform, indicating that the value is not a number.
Example
Opening this small page:
<HTML>
<BODY>
<SCRIPT Language = "JavaScript">
string1 = "-1.987654321"
string2 = "$500.00"
document.write ('parseInt(string1) = ',parseInt(string1))
document.write ('<BR>')
document.write ('parseInt(string2) = ',parseInt(string2))
</SCRIPT>
</BODY>
</HTML>This displays this in the Web browser screen:
parseInt(string1) = -1
parseInt(string2) = 0
The unescape() function returns the ASCII string for an escaped character.
Syntax
The syntax for this function is
unescape("string")
where string is a string containing characters in either of the following forms:
"%integer", where integer is a number between 0 and 255 (decimal).
"hex", where hex is a number between 0x0 and 0xFF (hexadecimal).
Description
The unescape function generally is used to convert a string that has been escaped using the escape() function to normal text. For example, you might escape a string to store it in a document cookie. Upon retrieving that string, you'd use the unescape() function to convert the string back to regular English.
Example
This small page stores an escaped string in a variable named Encoded and displays that text on the screen. A second document.write statement then displays the unescaped version of the Encoded variable:
<HTML>
<BODY>
<SCRIPT Language = "JavaScript">
var English = "Hello!"
var Encoded = escape(English)
document.write ("Encoded = ",Encoded,'<br>')
document.write ("unescape(Encoded) = ",unescape(Encoded))
</SCRIPT>
</BODY>
</HTML>The result on the screen is

|
|
![]() |