Numbers
The topics are:
Number Literal Syntax
Numbers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8.)
Note
For C/C++ programmers: Numbers have the same syntax as C and C++ integers and doubles. They are internally represented as 64-bit double precision floating-point numbers. |
A decimal number consists of a sequence of digits, followed by an optional fraction, followed by an optional exponent. The fraction consists of a decimal point (.) followed by a sequence of digits; the exponent consists of an e or E followed by an optional + or - sign and a sequence of digits. A decimal number must have at least one digit.
Here are some examples of decimal number literals:
15
3.14
4e100
.25
5.25e-10
A hexadecimal number consists of 0x or 0X prefix, followed by a sequence of hexadecimal digits, which include digits (0-9) and the letters a-f or A-F. For example:
0x3ff
0x0
An octal number consists of a 0 followed by a sequence of octal digits, which include the digits 0-7. For example:
0123
0777
Special Numbers
There are three special numbers: NaN (Not-A-Number), Infinity (positive infinity), and ‑Infinity (negative infinity.)
The special number NaN is used to indicate errors in number manipulations. For example, the square root function Math.sqrt applied to a negative number returns NaN. There is no representation of NaN as a number literal, but the global variable NaN contains its value.
The NaN value is contagious, and a numeric operation involving NaN always returns NaN. A comparison operation involving NaN always returns false—even the NaN == NaN comparison.
Examples:
Math.sqrt(-1) -> NaN
Math.sqrt(NaN) -> NaN
NaN + 3 -> NaN
NaN == NaN -> false
NaN <= 3 -> false
NaN >= 3 -> false
The special numbers Infinity and -Infinity are used to indicate infinite values and overflows in arithmetic operations. The global variable Infinity contains the positive infinity. The negative infinity can be computed using the negation operator (‑Infinity).
Examples:
1/0-> Infinity
-1/0 -> -Infinity
1/Infinity -> 0
Infinity == Infinity -> true
Automatic Conversion to a Number
When a function or a method which expects a number as one of its arguments is passed a non-numeric value, it tries to convert this value to a number using the following rules:
-
A Strings is parsed as a number literal. If the string does not represent a valid number literal, the conversion yields NaN.
-
The Booleans true yields the number 1;
-
The Booleans false yields the number 0;
-
The The null Value value yields the number 0;
-
A Dates yields the corresponding number of milliseconds since 00:00:00 UTC, January 1, 1970.
For example, if the Math.sqrt function is passed a string, this string is converted to the number it represents:
Math.sqrt("25") -> 5
Similarly, operators which take numeric operands attempt to convert any non-numeric operands to a number:
"3" * "4"-> 12
For operators which can take both strings and numbers, such as +, the conversion to a string takes precedence over the conversion to a number (see Automatic Conversion to a String .) In other words, if at least one of the operands is a string, the other operand is converted to a string; if none of the operands is a string, the operands are both converted to numbers. For example:
"3" + true -> "3true"
3 + true -> 4
For comparison operators, such as == and >=, the conversion to a number takes precedence over the conversion to a string. In other words, if at least one of the operands is a number, the other operand is converted to a number. If both operands are strings, the comparison is made on strings. For example:
"10" > "2" -> false
"10" > 2 -> true
Number Methods
The only number method is:
Syntax |
Effect |
Returns a string representing the number as a literal. Example: (14.3e2).toString() -> "1430" |
Numeric Functions
The following numeric functions are defined:
Note
For C/C++ programmers: Most of these functions are wrap-ups for standard math library functions. |
Numeric Constants
The following numeric constants are defined:
Syntax |
Value |
Contains the NaN value. |
|
Contains the Infinity value. |
|
Same as NaN. |
|
The maximum representable number, approximately 1.79E+308. |
|
The smallest representable positive number, approximately 2.22E-308. |
|
Euler's constant and the base of natural logarithms, approximately 2.718. |
|
The natural logarithm of 10, approximately 2.302. |
|
The natural logarithm of two, approximately 0.693. |
|
The base 2 logarithm of e, approximately 1.442. |
|
The base 10 logarithm of e, approximately 0.434. |
|
The ratio of the circumference of a circle to its diameter, approximately 3.142. |
|
The square root of one-half, approximately 0.707. |
|
The square root of two, approximately 1.414. |
Numeric Operators
The following numeric operators are available:
Note
For C/C++ programmers: These operators are the same as in C and C++. |
Syntax |
Effect |
x + y |
The usual arithmetic operations. Examples: 3 + 4.2-> 7.2 100 - 120-> -20 4 * 7.1-> 28.4 6 / 5-> 1.2 |
- x |
Negation. Examples: - 142-> -142 |
x % y |
Returns the floating-point remainder of dividing x by y. Examples: 12 % 5-> 2 12.5 % 5-> 2.5 |
x == y |
The operator == returns true if x and y are equal, and false otherwise. The operator != is the converse of ==. Examples: 12 == 12 -> true 12 == 12.1-> false 12 != 12.1-> true |
x < y |
The operator < returns true if x is smaller than y, and false otherwise. The operator <= returns true if x is smaller or equal to y, and false otherwise; and so on. Examples: -1 < 0-> true 1 < 1-> false 1 <= 1-> true |
x & y |
The bitwise operations AND, OR, and XOR. X and y must be integers in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.) Examples: 14 & 9-> 8 (1110 & 1001 -> 1000) 14 | 9-> 15 (1110 | 1001 -> 1111) 14 ^ 9-> 7 (1110 ^ 1001 -> 111) |
~ x |
Bitwise NOT. X must be an integer in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.) Examples: ~ 14 -> 1 (~ 1110 -> 0001) |
x << y |
Binary shift operations. X and y must be integers in the range of -2**32+1 to 2**32-1 (-2147483647 to 2147483647.) The operator << shifts to the left, >> shifts to the right (maintaining the sign bit), and >>> shifts to the right, shifting in zeros from the left. Examples: 9 << 2 -> 36 (1001 << 2 -> 100100) 9 >> 2 -> 2 (1001 >> 2 -> 10) -9 >> 2 -> -2 (1..11001 >> 2 -> 1..11110) -9 >>> 2 -> 1073741821 (1..11001 >>> 2 -> 01..11110) |