JS: Math (namespace)
What is the Keyword “Math”
Math is the value of the property key "Math" of the Global Object
.
console.log(globalThis["Math"] === Math);
Type
typeof Math === "object"
Parent
Parent of Math is Object.prototype.
Reflect.getPrototypeOf(Math) === Object.prototype
Purpose
Purpose of Math object is as a namespace for math related functions and constants.
Properties
Constants
Math.PI-
π. Value is ≈
3.1415 Math.E-
Constant e. The number Limit[(1+1/n)^n,{n,∞}] . Value is ≈
2.7182 Math.LN10-
(new in JS: ECMAScript 2015) natural log of 10. Value is ≈
2.3025 Math.LN2-
natural log of 2. Value is ≈
0.6931 Math.LOG10E-
base-10 log of e. Value is ≈
0.4342 Math.LOG2E-
base-2 log of e. Value is ≈
1.4426 Math.SQRT1_2-
1/(√2). Value is ≈
0.7071 Math.SQRT2-
√2. Value is ≈
1.4142 Math[Symbol.toStringTag]-
(new in JS: ECMAScript 2015) value is the string
"Math".
Common Math Functions
Math.max(args)-
Return the largest of the arguments.
const xx = [24, 185, 53]; console.log(Math.max(...xx)); // 185 Math.min(args)-
Return the smallest of the arguments. e.g.
Math.min(...xArray) Math.random()-
Return a real number between 0 to 1, including 0 but excluding 1. 〔see Random Integer Function 📜〕
Math.sign(x)-
Return
1if x is positive,-1if negative,0if positive 0,-0if negative 0. (JavaScript's 0 can are signed.-0 === 0eval totrue.) Math.abs(x)-
Absolute value.
Ceiling, Floor, Round
Math.ceil(x)-
Return a integer that's equal or greater than x.
Math.floor(x)-
Return a integer that's equal or less than x.
Math.round(x)-
Return a number to the nearest integer. 0.5 rounds up to 1.
Math.fround(x)-
(new in JS: ECMAScript 2015) Return the nearest 32bits representation of x.
Math.trunc(x)-
(new in JS: ECMAScript 2015) Return the integral part of x.
Power and Roots
Math.pow(x, y)-
- x raised to the power of y.
- Same as
x ** y.
Math.sqrt(x)-
Square root.
Math.cbrt(x)-
(new in JS: ECMAScript 2015) Cube root.
To compute nth root, use Math.pow(x, 1/n).
Exponential and Logarithm
Math.exp(x)-
e raised to the power of x. Same as
e ** x. Math.expm1(x)-
(new in JS: ECMAScript 2015) Same as
e ** x -1. The result is computed in a way that is accurate even when the value of x is close 0.
console.log(Math.expm1(0.01)); // 0.010050167084168058 console.log(Math.pow(Math.E, 0.01) - 1); // 0.010050167084167949
Math.log(x)-
Natural log of x
Math.log10(x)-
(new in JS: ECMAScript 2015) Base 10 log of x
Math.log1p(x)-
(new in JS: ECMAScript 2015) Natural log of
x+1 Math.log2(x)-
(new in JS: ECMAScript 2015) Base 2 log of x
Trig
Math.sin(x)-
Sine function.
Math.cos(x)-
Cosine function.
Math.tan(x)-
Tangent function.
Math.asin(x)-
Inverse of sine. Domain is -1 to 1. Range is 0 to π.
Math.acos(x)-
Inverse of cosine. Domain is -1 to 1. Range is 0 to π.
Math.atan(x)-
Inverse of tangent. No restriction on domain. Range is -π/2 to π/2.
Math.atan2(y, x)-
Return the arc tangent of the quotient y/x, where the signs of y and x are used to determine the quadrant of the result. Result is in range from −π to +π.
Math.hypot(x1, x2, etc)-
(new in JS: ECMAScript 2015) Return the square root of
(x1^2 + x2^2 + etc^2). This is useful to compute the length of a higher dimension vector in linear algebra.
All trig functions's argument are radians.
- To convert degree d to radians, do:
d*Math.PI/180 - To convert degree r to degrees, do:
r*180/Math.PI
Hyperbolic Functions
Math.sinh(x)-
(new in JS: ECMAScript 2015) Hyperbolic sine. That is
(e^x - e^(-x))/2 Math.cosh(x)-
(new in JS: ECMAScript 2015) hyperbolic cosine. That is
(e^x + e^(-x))/2 Math.tanh(x)-
(new in JS: ECMAScript 2015) Hyperbolic tangent. That is
Sinh[x]/Cosh[x] Math.asinh(x)-
(new in JS: ECMAScript 2015) Inverse hyperbolic sine.
Math.acosh(x)-
(new in JS: ECMAScript 2015) Inverse hyperbolic cosine.
Math.atanh(x)-
(new in JS: ECMAScript 2015) Inverse hyperbolic tangent.
Misc
Math.clz32(x)-
(new in JS: ECMAScript 2015) Return the number of leading zero bits in the 32-bit binary representation of x.
Math.imul(a, b)-
(new in JS: ECMAScript 2015) multiplies a and b as though they were 32 bit signed integers.
JavaScript. Number
- JS: Number
- JS: Number Input. Binary, Hexadecimal, Octal, Etc
- JS: Assignment Operators
- JS: Math Operators
- JS: Number Comparison Operators
- JS: Convert String and Number
- JS: Convert Decimal, Hexadecimal
- JS: Convert Decimal, Binary
- JS: Format Number
- JS: Random Integer Range Function 📜
- JS: Number (class)
- JS: Number Constructor
- JS: Number.prototype
- JS: Math (namespace)