Undefined Data Type
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.
Null Data Type
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;
Boolean Data Type
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);
Number Data Type
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.
- Integers ― Integers are positive and negative numbers that do not have a fractional part.
- Real numbers ― Real numbers can have both an integer and a fractional part.
- Scientific notation ― Scientific notation is useful for representing extremely large and extremely small values. Values represented in scientific notation are formatted as a coefficient, followed by the letter “e”, followed by an exponent. The coefficient can be an integer or real number, and the letter “e” can be either lowercase or uppercase. The exponent, however, must be an integer.
- Hexadecimals ― Hexadecimal (or hex) values are base sixteen integers. Hexadecimal uses the ten decimal digits 0-9 and the letters A-F to represent numbers. Hex is often used as shorthand for representing binary values. Hexadecimal values must begin with the characters “0x”. Hex values are not case sensitive. Therefore, the values
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;
- Any finite number added to, or subtracted from, Infinity is Infinity.
- Any finite number added to, or subtracted from, -Infinity is -Infinity.
- Adding Infinity and -Infinity yields NaN.
- Any positive value (including Infinity) multiplied by Infinity is Infinity.
- Any positive value (including Infinity) multiplied by -Infinity is -Infinity.
- Any negative value (including -Infinity) multiplied by -Infinity is Infinity.
- Any negative value (including -Infinity) multiplied by Infinity is -Infinity.
- Zero multiplied by Infinity or -Infinity is NaN.
- Any finite value divided by Infinity or -Infinity is zero.
- Infinity divided by any finite positive value is Infinity.
- Infinity divided by any finite negative value is -Infinity.
- -Infinity divided by any finite negative value is Infinity.
- -Infinity divided by any finite positive value is -Infinity.
- Infinity divided by Infinity or -Infinity is NaN.
- -Infinity divided by Infinity or -Infinity is NaN.
- Any positive value (including Infinity) divided by zero is Infinity.
- Any negative value (including -Infinity) divided by zero is -Infinity.
String Data Type
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.
Things to Remember
- The primitive data types are Undefined, Null, Boolean, Number, and String.
- The Undefined type represents an absent or unknown of value.
- The Null type represents an intentional lack of value.
- Boolean values can hold two values, true and false.
- Numeric data is stored in the IEEE-754 floating point format.
- The String type is used to store textual data.
If you like this article, go ahead and follow me on Twitter or buy me a coffee to support my work!