"Happy am I; from care I'm free!" '"Avast, ye lubbers!" roared the technician.' "45" 'c'
You denote hexadecimal ("hex") integers by prefixing them with a leading "0x" (zero and x|X). They can contain digits 0 through 9, and letters A through F (either uppercase or lowercase) only. The letters A through F are used to represent, as single digits, 10 through 15 in base 10. That is, 0xF is equivalent to 15, and 0x10 is equivalent to 16.
You denote octal integers by prefixing them with a leading "0" (zero). They can contain digits 0 through 7 only. A number that has a leading "0" and contains the digits "8" and/or "9" is interpreted as a decimal number.
Both hexadecimal and octal numbers can be negative, but they cannot have a decimal portion, and they cannot be written in scientific (exponential) notation.
Starting in Internet Explorer 9 standards mode, the parseInt function does not treat a string that has a prefix of "0" as octal. When you are not using the parseInt function, however, strings with a prefix of "0" can still be interpreted as octal.
.0001, 0.0001, 1e-4, 1.0e-4
Four equivalent floating-point numbers.
A floating-point number.
An integer. Although this looks like an octal number (it begins with a zero), 8 is not a valid octal digit, so the number is treated as a decimal.
An octal integer. Notice that although it only appears to be one less than the number above, its actual value is quite different.
A floating point number. Even though this begins with a zero, it is not an octal number because it has a decimal point.
This is an error. The two leading zeros mark the number as an octal, but octals are not allowed a decimal component.
N/A (compiler error)
A hexadecimal integer.
A hexadecimal integer.
A hexadecimal integer. Notice that the 'e' is not treated as exponentiation.
This is an error. Hexadecimal numbers cannot have decimal parts.
N/A (compiler error)
NaN (not a number). This is used when a mathematical operation is performed on inappropriate data, such as strings or the undefined value
Whereas the string and number data types can have a virtually unlimited number of different values, the Boolean data type can only have two. They are the literals true and false. A Boolean value is a truth-value — it expresses the validity of a condition (tells whether the condition is true or not).
y = (x == 2000);
Here, the value of the variable x is tested to see if it is equal to the number 2000. If it is, the result of the comparison is the Boolean value true, which is assigned to the variable y. If x is not equal to 2000, then the result of the comparison is the Boolean value false.
if (x == 2000) z = z + 1; else x = x + 1;
You can use any expression as a comparative expression. Any expression that evaluates to 0, null, undefined, or an empty string is interpreted as false. An expression that evaluates to any other value is interpreted as true. For example, you could use an expression such as:
// This may not do what you expect. See below! if (x = y + z)
Note that the above line does not check if x is equal to y + z, since only a single equal sign (assignment) is used. Instead, the code above assigns the value of y + z to the variable x, and then checks if the result of the entire expression (the value of x) is zero. To check if x is equal to y + z, use the following code.
// This is different from the code above! if (x == y + z)
For more information on comparisons, see Controlling Program Flow.
A variable that contains null contains "no value" or "no object." In other words, it holds no valid number, string, Boolean, array, or object. You can erase the contents of a variable (without deleting the variable) by assigning it the null value.
The undefined value is returned when you use:
an object property that does not exist,
a variable that has been declared, but has never had a value assigned to it.
Notice that you cannot test to see if a variable exists by comparing it to undefined, although you can check if its type is "undefined". In the following code example, assume that the programmer is trying to test if the variable x has been declared:
// This method will not work if (x == undefined) // do something // This method also won't work - you must check for // the string "undefined" if (typeof(x) == undefined) // do something // This method will work if (typeof(x) == "undefined") // do something
Consider comparing the undefined value to null.
someObject.prop == null;
This comparison is true,
if the property someObject.prop contains the value null,
if the property someObject.prop does not exist.
To check if an object property exists, you can use the new in operator:
if ("prop" in someObject) // someObject has the property 'prop'