files/en-us/web/javascript/reference/global_objects/math/index.md
The Math namespace object contains static properties and methods for mathematical constants and functions.
Math works with the {{jsxref("Number")}} type. It doesn't work with {{jsxref("BigInt")}}.
Unlike most global objects, Math is not a constructor. You cannot use it with the new operator or invoke the Math object as a function. All properties and methods of Math are static.
[!NOTE] Many
Mathfunctions have a precision that's implementation-dependent.This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!
2.718.10; approximately 2.303.2; approximately 0.693.E; approximately 0.434.E; approximately 1.443.3.14159.0.707.2; approximately 1.414.Math[Symbol.toStringTag]
[Symbol.toStringTag] property is the string "Math". This property is used in {{jsxref("Object.prototype.toString()")}}.2.718…, the base of the natural logarithm).1 from exp(x).1 + x for the number x.x to the exponent power y (that is, x<sup><code>y</code></sup>).0 and 1.The trigonometric functions sin(), cos(), tan(), asin(), acos(), atan(), and atan2() expect (and return) angles in radians.
Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
function radToDeg(rad) {
return rad / (Math.PI / 180);
}
If we want to calculate the height of an equilateral triangle, and we know its side length is 100, we can use the formulae length of the adjacent multiplied by the tangent of the angle is equal to the opposite.
In JavaScript, we can do this with the following:
50 * Math.tan(degToRad(60));
We use our degToRad() function to convert 60 degrees to radians, as {{jsxref("Math.tan()")}} expects an input value in radians.
This can be achieved with a combination of {{jsxref("Math.random()")}} and {{jsxref("Math.floor()")}}:
function random(min, max) {
const num = Math.floor(Math.random() * (max - min + 1)) + min;
return num;
}
random(1, 10);
{{Specifications}}
{{Compat}}