Math : Object
Contains math related constants and functions.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8
----
E : Number
The constant e = 2.718...
console.log(Math.E);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.1
ReadOnly:
true
----
LN10 : Number
The value of the natural log of 10.
It is the reciprocal of %%#LOG10E|**LOG10E**%%.
Useful for computing the %%#log|log%% base 10 of a number.
See also %%#log10|**log10()**%%.
console.log(Math.LN10);
console.log(Math.log(10));
console.log(1 / Math.LOG10E);
var log10 = function(x) {
return Math.log(x) / Math.LN10;
};
console.log(log10(10000));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.2
ReadOnly:
true
----
LN2 : Number
The value of the natural log of 2.
It is the reciprocal of %%#LOG2E|**LOG2E**%%.
Useful for computing the %%#log|log%% base 2 of a number.
See also %%#log2|**log2()**%%.
console.log(Math.LN2);
console.log(Math.log(2));
console.log(1 / Math.LOG2E);
var log2 = function(x) {
return Math.log(x) / Math.LN2;
};
console.log(log2(64));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.3
ReadOnly:
true
----
LOG2E : Number
The value of the log base 2 of e.
It is the reciprocal of %%#LN2|**LN2**%%.
Useful for computing the %%#log|log%% base 2 of a number.
See also %%#log2|**log2()**%%.
console.log(Math.LOG2E);
console.log(Math.log(Math.E) / Math.log(2));
console.log(1 / Math.LN2);
var log2 = function(x) {
return Math.log(x) * Math.LOG2E;
};
console.log(log2(32));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.4
ReadOnly:
true
----
LOG10E : Number
The value of the log base 10 of e.
It is the reciprocal of %%#LN10|**LN10**%%.
Useful for computing the %%#log|log%% base 10 of a number.
See also %%#log10|**log10()**%%.
console.log(Math.LOG10E);
console.log(Math.log(Math.E) / Math.log(10));
console.log(1 / Math.LN10);
var log10 = function(x) {
return Math.log(x) * Math.LOG10E;
};
console.log(log10(10000));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.5
ReadOnly:
true
----
PI : Number
The constant pi = 3.14...
console.log(Math.PI);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.6
ReadOnly:
true
----
SQRT1_2 : Number
The value of **1 / %%#sqrt|sqrt%%(2)**.
console.log(Math.SQRT1_2);
console.log(1 / Math.sqrt(2));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.7
ReadOnly:
true
----
SQRT2 : Number
The value of **%%#sqrt|sqrt%%(2)**.
console.log(Math.SQRT2);
console.log(Math.sqrt(2));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.1.8
ReadOnly:
true
----
abs(x : Number) : Number
Returns the absolute value of **x**.
console.log(Math.abs(10));
console.log(Math.abs(-10));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.1
----
acos(x : Number) : Number
Returns the angle (in radians between 0 and pi) whose cosine is **x**.
See also %%#cos|**cos()**%%.
console.log(Math.acos(1));
console.log(Math.acos(0));
console.log(Math.acos(-1));
var acosDegrees = function(x) {
return Math.acos(x) * 180 / Math.PI;
};
console.log(acosDegrees(0));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.2
----
acosh(x : Number) : Number
Returns the hyperbolic angle whose hyperbolic cosine is **x**.
See also %%#cosh|**cosh()**%%.
console.log(Math.acosh(1));
console.log(Math.acosh(Math.cosh(3)));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.acosh
----
asin(x : Number) : Number
Returns the angle (in radians between -pi/2 and pi/2) whose sine is **x**.
See also %%#sin|**sin()**%%.
console.log(Math.asin(1));
console.log(Math.asin(0));
console.log(Math.asin(-1));
var asinDegrees = function(x) {
return Math.asin(x) * 180 / Math.PI;
};
console.log(asinDegrees(1));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.3
----
asinh(x : Number) : Number
Returns the hyperbolic angle whose hyperbolic sine is **x**.
See also %%#sinh|**sinh()**%%.
console.log(Math.sinh(0));
console.log(Math.asinh(Math.sinh(3)));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.asinh
----
atan(x: Number) : Number
Returns the angle (in radians between -pi/2 and pi/2) whose tangent is **x**.
See also %%#atan2|**atan2()**%% and %%#tan|**tan()**%%.
console.log(Math.atan(1));
console.log(Math.atan(0));
console.log(Math.atan(-1));
var atanDegrees = function(x) {
return Math.atan(x) * 180 / Math.PI;
};
console.log(atanDegrees(1));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.4
----
atanh(x : Number) : Number
Returns the hyperbolic angle whose hyperbolic tangent is **x**.
See also %%#tanh|**tanh()**%%.
console.log(Math.atanh(0));
console.log(Math.atanh(Math.tanh(0.5)));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.atanh
----
atan2(y : Number, x : Number) : Number
Returns the angle (in radians between -pi and pi) between the positive x axis and
the line segment from the origin to the point at (**x**, **y**).
See also %%#atan|**atan()**%% and %%#tan|**tan()**%%.
console.log(Math.atan2(1, 0));
console.log(Math.atan2(0, 1));
console.log(Math.atan2(-1, 0));
console.log(Math.atan2(0, -1));
var atan2Degrees = function(y, x) {
return Math.atan2(y, x) * 180 / Math.PI;
};
console.log(atan2Degrees(1, 0));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.5
----
cbrt(x : Number) : Number
Returns cube root of **x**.
console.log(Math.cbrt(8));
console.log(Math.cbrt(27));
console.log(Math.cbrt(1234));
console.log(Math.pow(1234, 1 / 3));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.cbrt
----
ceil(x: Number) : Number
Returns the next integer greater than or equal to **x**.
See also %%#round|**round()**%%, %%#floor|**floor()**%%, and %%#trunc|**trunc()**%%.
console.log(Math.ceil(1.9));
console.log(Math.ceil(2));
console.log(Math.ceil(-1.9));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.6
----
clz(x : Number) : Number
Returns the number of leading **0** bits in the 32 bit unsigned integer
representation of **x**.
console.log(Math.clz32(0x00000000));
console.log(Math.clz32(0x08000000));
console.log(Math.clz32(0xFFFFFFFF));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.clz
----
cos(x: Number) : Number
Returns the cosine of **x** (in radians).
See also %%#acos|**acos()**%%.
console.log(Math.cos(0));
console.log(Math.cos(Math.PI / 2));
console.log(Math.cos(Math.PI));
var cosDegrees = function(x) {
return Math.cos(x * Math.PI / 180);
};
console.log(cosDegrees(45));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.7
----
cosh(x : Number) : Number
Returns the hyperbolic cosine of **x**.
See also %%#acosh|**acosh()**%%.
console.log(Math.cosh(0));
console.log(Math.cosh(3));
console.log((Math.exp(3) + Math.exp(-3)) / 2);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.cosh
----
exp(x: Number) : Number
Returns %%#E|e%% raised to the **x** power.
See also %%#log|**log()**%% and %%#expm1|**expm1()**%%.
console.log(Math.exp(2));
console.log(Math.E * Math.E);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.8
----
expm1(x : Number) : Number
Returns %%#E|e%% raised to the **x** power minus 1.
This is more accurate than using **%%#exp|exp%%(x) - 1**
when **x** is close to **0**.
console.log(Math.expm1(0));
console.log(Math.exp(0) - 1);
console.log(Math.expm1(1));
console.log(Math.exp(1) - 1);
console.log(Math.expm1(1e-9));
console.log(Math.exp(1e-9) - 1);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.expm1
----
floor(x: Number) : Number
Returns the previous integer less than or equal to **x**.
See also %%#round|**round()**%%, %%#ceil|**ceil()**%%, and %%#trunc|**trunc()**%%.
console.log(Math.floor(1.9));
console.log(Math.floor(2));
console.log(Math.floor(-1.9));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.9
----
fround(x : Number) : Number
Returns the closest single precision (32 bit) floating point number
to **x**.
console.log(Math.fround(1.1));
// Values are automatically rounded when put in a Float32Array
var float32Array = new Float32Array(1);
float32Array[0] = 1.1;
console.log(float32Array[0]);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.fround
----
hypot(x : Number, y : Number, [z : Number, [...]]) : Number
Returns length of a vector with components [**x**, **y**, ...].
This is the square root of the sum of the squares of the
components.
console.log(Math.hypot(3, 4));
console.log(Math.sqrt(3 * 3 + 4 * 4));
console.log(Math.hypot(5, 12));
console.log(Math.sqrt(5 * 5 + 12 * 12));
var myVector = [3, 2, 8];
console.log(Math.hypot(...myVector));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.hypot
----
imul(x : Number, y : Number) : Number
Returns the product of **x** and **y** treating both
values as 32 bit integer values and truncating the result
to a 32 bit integer value.
console.log(Math.imul(3, 4));
console.log(Math.imul(-2, 9));
// The following overflows
console.log(Math.imul(0x40000000, 2).toString(16));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.imul
----
log(x : Number) : Number
Returns the natural logarithm (base %%#E|e%%) of **x**. To compute the
logarithm with respect to a different base **b**, use the
formula **Math.log(x) / Math.log(b)**.
For base 10, you can use
**Math.log(x) * %%#LOG10E|Math.LOG10E%%**
(or %%#log10|**log10()**%% with ECMAScript 2015).
For base 2, you can use
**Math.log(x) * %%#LOG2E|Math.LOG2E%%**
(or %%#log2|**log2()**%% with ECMAScript 2015).
See also %%#exp|**exp()**%% and %%#log1p|**log1p()**%%.
var x = Math.exp(2);
console.log(x);
console.log(Math.log(x));
var logb = function(x, b) {
return Math.log(x) / Math.log(b);
};
var log10 = function(x) {
return Math.log(x) * Math.LOG10E;
};
console.log(logb(10000, 10));
console.log(log10(10000));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.10
----
log1p(x : Number) : Number
Returns the natural logarithm (base %%#E|e%%) of **x + 1**.
This is more accurate than using **%%#log|log%%(x + 1)**
when **x** is close to **0**.
console.log(Math.log(1 + 1));
console.log(Math.log1p(1));
console.log(Math.log(1e-9 + 1));
console.log(Math.log1p(1e-9));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.log1p
----
log2(x : Number) : Number
Returns the base 2 logarithm of **x**.
See also %%#log|**log()**%%, %%#LOG2E|**LOG2E**%%, and
%%#LN2|**LN2**%%.
console.log(Math.log2(16));
console.log(Math.log(16) * Math.LOG2E);
console.log(Math.log(16) / Math.LN2);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.log2
----
log10(x : Number) : Number
Returns the base 10 logarithm of **x**.
See also %%#log|**log()**%%, %%#LOG10E|**LOG10E**%%, and
%%#LN10|**LN10**%%.
console.log(Math.log10(1000));
console.log(Math.log(1000) * Math.LOG10E);
console.log(Math.log(1000) / Math.LN10);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.log10
----
max(x1 : Number, x2: Number, [...]) : Number
Returns the largest number among the parameters.
var x = 3, y = 1, z = 4;
console.log(Math.max(x, y, z));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.11
----
min(x1 : Number, x2 : Number, [...]) : Number
Returns the smallest number among the parameters.
var x = 3, y = 1, z = 4;
console.log(Math.min(x, y, z));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.12
----
pow(x : Number, y : Number) : Number
Returns **x** raised to the **y** power.
console.log(Math.pow(2, 3));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.13
----
random() : Number
Returns a random number between **0** (inclusive) and **1** (exclusive).
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
// Random integer between min (inclusive) and max (exclusive)
var randomInt = function(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
};
console.log(randomInt(0, 100));
console.log(randomInt(0, 100));
console.log(randomInt(0, 100));
// Produce a normally distributed random number using the Box-Muller transform
var randn = function() {
if (randn.hasValues) {
randn.hasValues = false;
return randn.distance * Math.sin(randn.angle);
}
var random = Math.max(Math.random(), 1e-100);
randn.distance = Math.sqrt(-2 * Math.log(random));
randn.angle = Math.random() * Math.PI * 2;
randn.hasValues = true;
return randn.distance * Math.cos(randn.angle);
}
console.log(randn());
console.log(randn());
console.log(randn());
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.14
----
round(x : Number) : Number
Returns the closest integer to **x**.
See also %%#floor|**floor()**%%, %%#ceil|**ceil()**%%, and %%#trunc|**trunc()**%%.
console.log(Math.round(2.2));
console.log(Math.round(2.5));
console.log(Math.round(-2.5));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.15
----
sign(x: Number) : Number
Returns **-1** if **x** is less than **0**, **0** if **x** is **0**,
and **1** if **x** is greater than **0**.
console.log(Math.sign(-9));
console.log(Math.sign(-0.3));
console.log(Math.sign(0));
console.log(Math.sign(0.8));
console.log(Math.sign(12));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.sign
----
sin(x: Number) : Number
Returns the sine of **x** (in radians).
See also %%#asin|**asin()**%%.
console.log(Math.sin(0));
console.log(Math.sin(Math.PI / 2));
console.log(Math.sin(Math.PI));
var sinDegrees = function(x) {
return Math.sin(x * Math.PI / 180);
};
console.log(sinDegrees(45));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.16
----
sinh(x : Number) : Number
Returns the hyperbolic sine of **x**.
See also %%#asinh|**asinh()**%%.
console.log(Math.sinh(0));
console.log(Math.sinh(3));
console.log((Math.exp(3) - Math.exp(-3)) / 2);
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.sinh
----
sqrt(x : Number) : Number
Returns the square root of **x**.
console.log(Math.sqrt(16));
console.log(Math.sqrt(49));
console.log(Math.sqrt(100));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.17
----
tan(x : Number) : Number
Returns the tangent of **x** (in radians).
See also %%#atan|**atan()**%%.
console.log(Math.tan(0));
console.log(Math.tan(Math.PI / 4));
console.log(Math.tan(Math.PI));
var tanDegrees = function(x) {
return Math.tan(x * Math.PI / 180);
};
console.log(tanDegrees(45));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.18
----
tanh(x : Number) : Number
Returns the hyperbolic tan of **x**.
See also %%#atanh|**atanh()**%%.
console.log(Math.tanh(0));
console.log(Math.tanh(3));
console.log((Math.exp(3) - Math.exp(-3)) / (Math.exp(3) + Math.exp(-3)));
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.tanh
----
trunc(x : Number) : Number
Returns the integer portion of **x**. This is the same as
%%#floor|**floor(x)**%% for positive numbers and
%%#ceil|**ceil(x)**%% for negative numbers.
See also %%#round|**round()**%%.
[2.2, -2.4, -2.6].forEach(function(x) {
console.log(x);
console.log(' trunc ' + Math.trunc(x));
console.log(' floor ' + Math.floor(x));
console.log(' ceil ' + Math.ceil(x));
console.log(' round ' + Math.round(x));
console.log();
});
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-math.trunc