ECMAScript Primitive Types
- Previous Page ECMAScript Values
- Next Page ECMAScript Type Conversion
ECMAScript has 5 primitive types, namely Undefined, Null, Boolean, Number, and String.
typeof operator
The typeof operator has one parameter, which is the variable or value to be checked. For example:
var sTemp = "test string"; alert (typeof sTemp); // Output: "string" alert (typeof 86); // Output: "number"
Calling typeof operator on a variable or value will return one of the following values:
- undefined - if the variable is of Undefined type
- boolean - if the variable is of Boolean type
- number - if the variable is of Number type
- string - if the variable is of String type
- object - if the variable is a reference type or Null type
Note:You may wonder why the typeof operator returns "Object" for null values. This is actually an error in the initial implementation of JavaScript, which was then carried forward by ECMAScript. Now, null is considered as a placeholder for objects, which explains this contradiction, but technically, it is still a primitive value.
Undefined type
As previously mentioned, the Undefined type has only one value, which is undefined. When a declared variable is not initialized, its default value is undefined.
var oTemp;
The previous line of code declares a variable oTemp without an initial value. This variable will be assigned the value undefined, which is the literal of the undefined type. The following code snippet can be used to test whether the value of the variable is equal to undefined:
var oTemp; alert(oTemp == undefined);
This code will output "true", indicating that these two values are indeed equal. The typeof operator can also be used to display that the value of the variable is undefined:
var oTemp; alert(typeof oTemp); // Outputs "undefined"
Tip:The value undefined is different from the undefined value. However, the typeof operator does not truly distinguish between these two values. Consider the following code:
var oTemp; alert(typeof oTemp); // Outputs "undefined" alert(typeof oTemp2); // Outputs "undefined"
The previous code outputs "undefined" for both variables, even though only the variable oTemp2 has never been declared. Using operators other than typeof on oTemp2 will cause an error because these operators can only be used on declared variables.
For example, the following code will cause an error:
var oTemp; alert(oTemp2 == undefined);
When a function does not have an explicit return value, it also returns the value "undefined", as shown below:
function testFunc() { {} alert(testFunc() == undefined); // Outputs "true"
Null type
Another type with only one value is Null, which has only one dedicated value, null, which is also its literal. The value undefined is actually derived from the value null, so ECMAScript defines them as equal.
alert(null == undefined); // Outputs "true"
Although these two values are equal, their meanings are different. undefined is the value assigned to a variable that has been declared but not initialized, while null is used to represent an object that does not exist (this point was briefly introduced when discussing the typeof operator). If a function or method is supposed to return an object, it usually returns null when the object cannot be found.
Boolean type
The Boolean type is one of the most commonly used types in ECMAScript. It has two values, true and false (which are also known as two Boolean literals).
Even though false is not equal to 0, 0 can be converted to false when necessary, making both safe to use in Boolean expressions.
var bFound = true; var bLost = false;
Number type
The most special type defined in ECMA-262 is the Number type. This type can represent 32-bit integers as well as 64-bit floating-point numbers.
Any number entered directly (instead of accessed from another variable) is considered a Number literal. For example, the following code declares a variable that stores an integer value, which is defined by the literal 86:
var iNum = 86;
Octal numbers and hexadecimal numbers
Integers can also be represented as octal literals (base 8) or hexadecimal literals (base 16). An octal literal must start with 0, and the subsequent digits can be any octal digit (0-7), as shown in the following code:
var iNum = 070; //070 is equal to decimal 56
To create a hexadecimal literal, the first digit must be 0, followed by the letter x, and then any hexadecimal digits (0 to 9 and A to F). These letters can be uppercase or lowercase. For example:
var iNum = 0x1f; //0x1f is equal to decimal 31 var iNum = 0xAB; //0xAB is equal to decimal 171
Tip:Although all integers can be represented as octal or hexadecimal literals, all mathematical operations return decimal results.
Floating-point numbers
To define a floating-point value, it must include a decimal point and at least one digit after the decimal point (for example, use 1.0 instead of 1). This is considered a floating-point literal. For example:
var fNum = 5.0;
An interesting aspect of floating-point literals is that before they are used in calculations, they are actually stored as strings.
Scientific notation
For very large or very small numbers, scientific notation can be used to represent floating-point numbers, and a number can be expressed as a digit (including decimal digits) followed by e (or E), and then followed by a multiple of 10. For example:
var fNum = 5.618e7
This symbol represents the number 56180000. Converting scientific notation to an expression will give you this value: 5.618 x 107.
Very small numbers can also be expressed in scientific notation, for example, 0.00000000000000008 can be represented as 8e-17(Here, 10 is raised to the -17th power, meaning it needs to be divided by 10 17 times). ECMAScript defaults to converting floating-point numbers with 6 or more leading zeros to scientific notation.
Tip:Floating-point values can also be stored in 64-bit IEEE 754 format, which means that decimal values can have a maximum of 17 decimal places. Values beyond 17 places are truncated, causing some minor mathematical errors.
Special Number values
Several special values are also defined as Number type. The first two are Number.MAX_VALUE and Number.MIN_VALUE, which define the outer boundaries of the Number value set. All ECMAScript numbers must be between these two values. However, the numerical results generated by calculations may not fall between these two values.
When the calculated number is greater than Number.MAX_VALUE, it is assigned the value Number.POSITIVE_INFINITY, meaning there are no longer any numerical values. Similarly, calculations that result in a value less than Number.MIN_VALUE are also assigned the value Number.NEGATIVE_INFINITY, also meaning there are no longer any numerical values. If the calculation returns an infinite value, the resulting value can no longer be used in other calculations.
In fact, there are specific values that represent infinity, as you might have guessed, namely Infinity. The value of Number.POSITIVE_INFINITY is Infinity. The value of Number.NEGATIVE_INFINITY is -Infinity.
Since infinity can be either positive or negative, a method can be used to determine if a number is finite (not just to test each infinite number separately). The isFinite() method can be called on any number to ensure that it is not infinite. For example:
var iResult = iNum * some_really_large_number; if (isFinite(iResult)) { alert("finite"); {} else { alert("infinite"); {}
The last special value is NaN, which stands for Not a Number. NaN is a peculiar special value. Generally speaking, this situation occurs when a type conversion (String, Boolean, etc.) fails. For example, converting the word 'blue' to a number will fail because there is no equivalent numerical value. Like infinity, NaN cannot be used in arithmetic calculations. Another oddity of NaN is that it is not equal to itself, which means the following code will return false:
alert(NaN == NaN); //Output: "false"
For this reason, it is not recommended to use the NaN value itself. The isNaN() function does quite well:
alert(isNaN("blue")); //Output: "true" alert(isNaN("666")); //Output: "false"
String Type
The unique feature of the String type is that it is the only primitive type without a fixed size. You can store 0 or more Unicode characters in a string, represented by 16-bit integers (Unicode is an international character set, which will be discussed later in this tutorial).
Each character in a string has a specific position, starting with the first character at position 0, the second character at position 1, and so on. This means that the position of the last character in the string is always one less than the length of the string:

String literals are declared by double quotes (" ) or single quotes ( ' ). Java declares strings with double quotes and characters with single quotes. However, since ECMAScript does not have a character type, either of these notations can be used. For example, the following two lines of code are both valid:
var sColor1 = "red"; var sColor2 = 'red';
The String type also includes several character literals, which Java, C, and Perl developers should be very familiar with.
The following lists the character literals of ECMAScript:
Literals | Meaning |
---|---|
\n | New line |
\t | Tab |
\b | Space |
\r | Carriage return |
\f | Form feed |
\\ | Backslash |
\' | Single quotes |
\" | Double quotes |
\0nnn | Octal code nnn Represents the character (n Is one of the octal digits from 0 to 7) |
\xnn | Hexadecimal code nn Represents the character (n Is one of the hexadecimal numbers from 0 to F) |
\unnnn | Hexadecimal code nnnn Represents the Unicode character (n Is one of the hexadecimal numbers from 0 to F) |
- Previous Page ECMAScript Values
- Next Page ECMAScript Type Conversion