Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|
The ECMA-262 standard defines the term Number Type as: a set of values representing numbers. In ECMAScript, the set of values represent the double-precision 64-bit format IEEE 754 values.
The Number data type is one of five ECMAScript primitive types. The other four are: String, Boolean, Null and Undefined. All primitive types have a definite (fixed) size except for the String type.
The Number type is distinguished from other types using a dynamic or weak typing method. The dynamic method involves interrogating the literal token for a range of values to determine whether it is a integer or floating point number.
var num22 = Number(22);
will behave like a conversion function and attempt to convert its argument to a number. If successful, the function returns a primitive Number type value and if unsuccessful, the function returns "NaN'.var num22 = new Number(22);
In OOP terms, the variable is an "instance" of the class "Number".
var num22 = Number(22);
var num22 = 22;
num22 = 22;
Statement | Result | Comment | ||
---|---|---|---|---|
1st operand | operator | 2nd operand | ||
0 | divide | 0 | NaN | Divide zero by zero will result in NaN. |
1 | divide | 0 | Infinity | Divide a positive number by zero will result in Infinity. |
-1 | divide | 0 | -Infinity | Divide a negative number by zero will result in negative Infinity. |
-Infinity | add | Infinity | NaN | Add Infinity to -Infinity will result in NaN. |
Infinity | == | Infinity | true | All Infinity values compare equal. |
-Infinity | == | -Infinity | true | All -Infinity values compare equal. |
numvar | = | Number(string) | type change | When a string contains all numbers, you can convert the type to a number by using the Number constructor as a function. |
numvar | = | string - 0 | type change | When a string contains all numbers, you can convert the type to a number by performing a numeric operation on it. |
numvar | = | Number(false) | 0 | Here is some more type changes where non-numeric values ( valid literals for other types) are cast as numeric types. |
numvar | = | Number(true) | 1 | |
numvar | = | Number(null) | 0 | |
numvar | = | Number("43") | 43 | |
numvar | = | Number("4.3") | 4.3 | |
numvar | = | Number(undefined) | NaN | More attempts at type casting; these two are errors. "Undefined" is not a valid literal and the string can not be converted into a legitimate float. |
numvar | = | Number("4.3.2") | NaN | |
biggest | = | Number.MAX_VALUE | MAX_VALUE | Assign the maximum allowed value to a variable. |
toobig | = | Number.MAX_VALUE + 1 | Infinity | Assigned a value too big for 64-bits. Any number larger than the maximum is POSITIVE_INFINITY. |
toosmall | = | Number.MIN_VALUE - 1 | -Infinity | Assigned a value too small for 64-bit limit; -Infinity and Number.NEGATIVE_INFINITY are the same value. |
hexnum | = | 0xf | 15 | Some hexadecimal examples. Hex digits may be either lower or uppercase. Remember:
0-9 equates to 0-9 decimal A equates to 10 decimal B equates to 11 decimal C equates to 12 decimal D equates to 13 decimal E equates to 14 decimal F equates to 15 decimal |
hexnum | = | 0x0 | 0 | |
hexnum | = | 0xFF | 255 | |
hexnum | = | 0x100 | 256 | |
hexnum | = | 0xfff | 4,095 | |
hexnum | = | 0x1000 | 4,096 | |
hexnum | = | 0xFFFF | 65,535 | |
hexnum | = | 0x10000 | 65,536 | |
hexnum | = | 0xFFFFF | 1,048,575 | |
hexnum | = | 0xABCDEF | 11,259,375 | |
afloat | = | 3.98E4 | 39800 | E-notation for a float; 3.98 X 10 X 10 X 10 X 10 |
5.3 | + | 3.7 | 9 | Two floats that make a whole number will give an integer as the result. |