The Math object allows you to perform mathematical tasks on numbers. Below is a beginners guide to the syntax and methods available.
Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it.
Math.round()
- Round a number to the nearest integer.
Math.round(2.5); // 3
Math.floor()
- Round a number downward to its nearest integer.
Math.floor(1.6); // 1
Math.ceil()
- Round a number upward to its nearest integer.
Math.ceil(1.4) // 2
Math.max()
- Return the number with the highest value from one or more numbers. Syntax Math.max(n1,n2,n3,...,nX)
.
Math.max(14, 10, 5, 20) // 20
Math.min()
- Return the number with the lowest value from one or more numbers. Syntax Math.min(n1,n2,n3,...,nX)
.
Math.min(14, 10, 5, 20) // 5
Math.trunc()
- Unlike other three Math methods: Math.floor()
, Math.ceil()
and Math.round()
, the way Math.trunc()
works is very simple and straightforward, just truncate (remove) the dot and the digits behind it, no matter whether the argument is a positive number or a negative number.
Math.trunc(1.213) // 1
Math.sign(x)
- Returns the sign of a number, indicating whether the number is positive, negative or zero.
Math.sign(-5) // -1
This function has 5 kinds of return values, 1, -1, 0, -0, NaN, which represent "positive number", "negative number", "positive zero", "negative zero" and NaN respectively.
Math.random()
- Returns a random number between 0 (included) and 1 (excluded).
Math.random() // e.g 0.39622215856797993
Commonly used in tandem with Math.round()
to generate random integers for random tests cases in Codewars. See Random Test Cases for Complete Beginners.
The process of using exponents is called "raising to a power", where the exponent is the "power".
Math.pow(x, y)
- Return the value of the number x
to be the power of y
.
Math.pow(5, 4) // 625
Math.sqrt(x)
- Returns the square root of number x
.
Math.sqrt(16) // 4
Math.cbrt(x)
- Returns the cube root of number x
.
Math.cbrt(16) // 2
Both square root and cube root can be replaced by Math.pow(x, y)
. To find roots of numbers you must have input y
as 1/y
. E.g.
Math.sqrt(16) // 4
Math.pow(16, 0.5) // 4
Math.cbrt(64) // 4
Math.pow(256, 0.25) // 4
Math.exp(x)
-Returns the value of E^x, where E is Euler's number (approximately 2.7183) and x
is the number passed to it.
Math.exp(1) // 2.718281828459045
Math.log(x)
-Returns the natural logarithm (base E) of number x
.
Math.log(2) // 0.6931471805599453
Trigonometry is a branch of mathematics that studies relationships involving lengths and angles of triangles. See more here.
Math.sin(x)
& Math.cos(x)
- Returns the sine/cosine of angle x
(in radians not degrees!).
Math.sin(3) // 0.1411200080598672
Math.cos(3) // -0.9899924966004454
Both methods return a value between -1 and 1. For more information on Sine & Cosine see here.
Math.asin(x)
& Math.acos(x)
- Returns the arcsine/arccosine of number x
.
Math.asin(0.5) // 0.5235987755982989
Math.acos(0.5) // 1.0471975511965979
The asin()
method returns the arcsine of a number as a value between -PI/2 and PI/2 radians. The acos()
method returns the arccosine of a number as a value value between 0 and PI radians. If the parameter x
is outside the range -1 to 1, the method will return NaN. For more information on arcsine & arccosine see here.
Math.tan(x)
- Returns the tangent of a number x
representing an angle (in radians).
Math.tan(90) // -1.995200412208242
Math.atan(x)
- Returns the arctangent of a number x
.
Math.atan(2) // 1.1071487177940904
The atan()
method returns the arctangent of a number as a value between -PI/2 and PI/2 radians.
Note that the trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2()) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180), and multiply by this to convert the other way.
Example kata:
Convert Between Radian and Degrees
As well as having methods, the Math object also allows you to access some mathematical contants as properties. There is a programming convention of writing constants in UPPERCASE as opposed to camelCase. So the Pi property of Math is written Math.PI
instead of Math.pi
or Math.Pi
. Constants are defined with the full precision of real numbers in JavaScript.
Math.E
- Euler's constant and the base of natural logarithms, approximately 2.718.
Math.LN2
- Natural logarithm of 2, approximately 0.693.
Math.LN10
- Natural logarithm of 10, approximately 2.303.
Math.LOG2E
-Base 2 logarithm of E, approximately 1.443.
Math.LOG10E
- Base 10 logarithm of E, approximately 0.434.
Math.PI
- Ratio of the circumference of a circle to its diameter, approximately 3.14159.
Math.SQRT1_2
- Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
Math.SQRT2
- Square root of 2, approximately 1.414.
There are a number of Math object resources on the web, the above is a simplied and combined version of the following resources: