JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)
Chapter 18. JavaScript Math
Math is a built-in object in JavaSript, which has methods and properties for math functions and constants, but not a function object. Math is not considered a constructor in JavaScript, unlike other global objects. The methods and properties of JS Math are static. We can consider the constant pi as Math.PI; we can refer to the sine as Math.sin(x). Take note that the X refers to the argument of the method. In JavaScript, constants are prescribed with complete precision, thanks to real numbers (as opposed to integers).
Properties of Math in JS
Refer to the table below for the different math properties used in JavaScript
Properties |
Description |
Math.E |
The Euler’s Constant, which is the base of natural logs, about 2.718 |
Math.LN10 |
The natural log of 10, about 2.303 |
Math.LN2 |
The natural log of 2, about 0.693 |
Math.LOG10E |
Base 10 log of E, about 0.434 |
Math.LOG2E |
Base 2 log of E, about 1.443 |
Math.SQRT2 |
Sq. root of 2, about 1.414 |
Math.SQRT1_2 |
Sq.root of ½ eq, 1 / sq. 2, about 0.707 |
Math.PI |
Ratio of circle circum to the diam about 3.1415 |
Methods
Take note that the trigo functions (atan2(), atan(), acos(), asin(), tan(), cos(), sin()) return radians angles. In order to conv radians into degrees, you can divide using Math.PI and multiply it for reverse conversion.
In addition, the precision of math functions in JS are dependent on implementation. Hence, various browsers will yield varying results. Even similar engines on a different architecture or operating system may yield varying results.
Refer to the table below for the different Math Methods in JS
Methods Math. |
Description |
.abs(x) |
Yields the absolute number value |
.acosh(x) |
Yields the number’s hyperbolic arcosine. |
.acos(x) |
Yields the number’s arcosine |
.asinh(x) |
Yields the number’s hyperbolic arcsine |
.asin(x) |
Yields the number’s arcsine |
.atanh(x) |
Yields the number’s hyperbolic arctangent |
.atan(x) |
Yields the number’s arctangent |
.atan2(x, y) |
Yields the quotient’s arctangent arguments |
.cbrt(x) |
Yields the number’s cube root |
.clz32(x) |
Yields the number prime zeros of a 32-bit int |
.ceil(x) |
Yields the min integer >= to x |
.cos(x) |
Yields the number’s cosine |
.coshh(x) |
Yields the number’s hyperbolic cosine |
.exp(x) |
Yields Ex (x=argument, E=Euler’s constant (log base) |
.floor(x) |
Yields the biggest int <= to x |
.fround(x) |
Yields the number’s nearest single precision float rep |
.hypot(x) |
Yields the sq root of the sum of sq of args |
.imul(x,y) |
Yields the result of multiplying 32-bit int |
.log(x) |
Yields the number’s natural log |
.log2(x) |
Yields the number’s base 2 log |
.log1p(x) |
Yields the number’s natural log of 1+x |
.log10(x) |
Yields the number’s base 10 log |
.max([x[,y[,-]]]) |
Yields the biggest zero or more numbers |
.min([x[,y[,-]]]) |
Yields the min zero or more numbers |
.pow(x. y) |
Yields base to exp power |
.random |
Yields a pseudo-random number between zero and 1 |
.sign(x) |
Yields the sign of the number, which indicates if x is zero, negative, or positive |
.round(x) |
Yields the number’s value rounded to the nearest int |
.sin(x) |
Yields the number’s sine |
.sinh(x) |
Yields the number’s hyperbolic sine |
.sqrt(x) |
Yields the number’s positive sq root |
.tan(x) |
Yields the number’s tangent |
.tanh(x) |
Yields the number’s hyperbolic tangent |
.trunc(x) |
Yields the number’s integral part, which removes any fractional digits |