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. |
Syntax | Effect |
number.toString( ) | Returns a string representing the number as a literal. Example: (14.3e2).toString() −> "1430" |
Note: For C/C++ programmers: Most of these functions are wrap-ups for standard math library functions. |
Syntax | Effect |
Math.abs(x) | Returns the absolute value of x. |
Math.max(x, y) Math.min(x, y) | Math.max(x, y) returns the larger of x and y, and Math.min(x, y) returns the lowest of the two. |
Math.random( ) | Returns a pseudo-random number between 0, inclusive, and 1, exclusive. |
Math.ceil(x) Math.floor(x) Math.round(x) | Math.ceil(x) returns the least integral value greater or equal to x. Math.floor(x) returns the greatest integral value less or equal to x. Math.round(x) returns the nearest integral value of x. |
Math.sqrt(x) | Returns the square root of x. |
Math.sin(x) Math.cos(x) Math.tan(x) Math.asin(x) Math.acos(x) Math.atan(x) Math.atan2(y, x) | Math.sin(x), Math.cos(x) and Math.tan(x) return trigonometric functions of radian arguments. Math.asin(x) returns the arc sine of x in the range -PI/2 to PI/2. Math.acos(x) returns the arc cosine of x in the range 0 to PI. Math.atan(x) returns the arc tangent of x in the range -PI/2 to PI/2. Math.atan2(y, x) converts rectangular coordinates (x, y) to polar (r, a)by computing a as an arc tangent of y/x in the range -PI to PI. |
Math.exp(x) Math.log(x) Math.pow(x, y) | Math.exp(x) computes the exponential function ex. Math.log(x) computes the natural logarithm of x. Math.pow(x, y) computes x raised to the power y. |
Syntax | Value |
NaN | Contains the NaN value. |
Infinity | Contains the Infinity value. |
Number.NaN | Same as NaN. |
Number.MAX_VALUE | The maximum representable number, approximately 1.79E+308. |
Number.MIN_VALUE | The smallest representable positive number, approximately 2.22E-308. |
Math.E | Euler's constant and the base of natural logarithms, approximately 2.718. |
Math.LN10 | The natural logarithm of 10, approximately 2.302. |
Math.LN2 | The natural logarithm of two, approximately 0.693. |
Math.LOG2E | The base 2 logarithm of e, approximately 1.442. |
Math.LOG10E | The base 10 logarithm of e, approximately 0.434. |
Math.PI | The ratio of the circumference of a circle to its diameter, approximately 3.142. |
Math.SQRT1_2 | The square root of one-half, approximately 0.707. |
Math.SQRT2 | The square root of two, approximately 1.414. |
Note: For C/C++ programmers: These operators are the same as in C and C++. |
Syntax | Effect |
x + y x - y x * y 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 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 x <= y x > y 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 x | y 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 x >> y 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) |