The Undefined data type represents an absent or unknown value. This data type consists of a single value, undefined. Variables which have not yet been assigned a value default to the undefined value. Variables can also intentionally be assigned the undefined value, although this is relatively uncommon*. The following example shows two variables which are both undefined. The first variable, “foo”, is undefined because no value is assigned to it. The second variable, “bar”, is intentionally set to undefined.
var foo, bar = undefined;
*The undefined value is not directly assigned. There is actually a global variable named
undefined whose value is the undefined value.
Much like the Undefined type, the Null data type also represents the absence of a value. Unlike Undefined, Null represents an intentional lack of a value. The Null data type is represented by a single literal,null. The null value is often used to initialize or clear object variables. The following example shows how the null value is assigned to a variable.
var foo = null;
The Boolean type consists of two literals, true and false, which correspond to the truth values of Boolean logic. Boolean variables are typically used to represent the results of a comparison (less than, greater than, etc.). They are also useful for representing the presence, or lack thereof, of a value. An example of this is a checkbox which is either checked or not checked. In the following example, the “isSet” variable is set to false. The example also stores the result of a comparison in “isGreater”. Since two is greater than one, “isGreater” is true.
var isSet = false, isGreater = (2 > 1);
All numeric data is represented by the Number data type. This type includes both negative and positive numbers. Negative numbers are always preceded by a minus sign. Positive integers can be preceded by a plus sign, but it is not required. Numbers can be formatted in a variety of ways. The following list describes each of these ways. All numbers are assumed to be base ten unless otherwise noted.
The following example shows how each number format is written.
var integer = 100, real = 3.14, // pi scientific = 3.14e2, // 314, or 3.14 * 10^2 octal = 0144, // base ten value is 100 hexadecimal = 0x64; // base ten value is 100
The Number type includes a special “Not-a-Number” (NaN) value which represents unrepresentable numbers. NaN can be used in mathematical computations, but any such computation will result in NaN. The following example includes three statements which evaluate to NaN. The first statement divides zero by zero. The result is undefined as a real number and is assigned the value NaN. The second statement computes the square root of a negative number. The result is an imaginary number, and is therefore treated as NaN. The third statement assigns the NaN value directly to a variable.
var foo = 0/0, // foo equals zero divided by zero bar = Math.sqrt(-1), // bar equals the square root of -1 baz = NaN;
var foo = "Hello World!", bar = 'Hello Again!', baz = "";
\t― Horizontal tab
\n― Line feed (new line)
\v― Vertical tab
\f― Form feed
\r― Carriage return
\"― Double quote
\'― Single quote
\xXX― Latin-1 encoded character specified by two hexadecimal digits. The hex value must be between
FF. For example,
\xA9represents the copyright symbol.
\uXXXX― Unicode character specified by four hexadecimal digits. For example, the copyright symbol is specified by
\XXX― Latin-1 encoded character specified by up to three octal digits. The octal value must be between 0 and 377. Like octal numbers, octal escape sequences are also prohibited in strict mode.
The following example creates a string literal which contains several common escape sequences. Note the escaped double quote characters and the new line escape sequence:
var str = "Say \"Hello World\"\nAnd start a new line";
The previous string looks somewhat convoluted. However, when the string is displayed, the escape sequences are replaced, resulting in the following output:
Say "Hello World" And start a new line
It is also possible to escape characters that do not have any special meaning. For example, the escape sequence
\c is simply replaced by the letter “c”. However, this should be avoided as there is no logical reason for doing so.