Math Object
The JavaScript Math
object comprises of several functions and constants that can be used to perform mathematical operations. It should be noted that the Math
object doesn't have a constructor.
Watch this video on the Math object
Some Math Properties
The syntax for using properties of the Math
object is Math.property
. A few of the properties available are listed next. Try them out in the console to see how they operate.
Math Property Examples
Math.LN2
- This will return the natural logarithm of 2.Math.LN10
- This will return the natural logarithm of 10.Math.LOG2E
- This will return base 2 logarithm of E.Math.LOG10E
- This will return base 10 logarithm of E.Math.E
- This will return Euler's number.Math.PI
- This will return Mathematical PI.Math.SQRT2
- This will return the square root of 2.Math.SQRT1_2
- This will return the square root of 1/2.
Math Functions
Math functions make it possible to carryout different computations. The syntax for using a Math function is Math.functionName(value)
. Example of Math functions includes Math.random(value)
, Math.round(value)
, Math.ceil(value)
, and Math.abs(value)
.
Watch this video on Math functions
Math.ceil()
This rounds a number up to the next largest integer.
Math.floor()
This rounds down a number to the next smallest integer.
Math.round()
This returns the number rounded to the nearest integer.
Math.trunc()
This returns the integer part of a number.
Math.max()
This returns the number with the highest value.
Math.min()
This returns the number with the lowest value.
Math.sqrt()
This returns the square root of a specified number.
Math.sin()
This returns the sine of an angle.
Math.cos()
This returns the cosine of an angle.
Math.tan()
This returns the tangent of an angle.
Math.abs()
This returns the absolute value of a number.
Math.pow()
This returns a number raised to a certain power.
Other examples of Math functions
Math.asin()
- This returns the arcsine of a number in radians.Math.acos()
- This returns the arccosine of a number in radians.Math.atan()
- This returns the arctangent of a number in radians.Math.sign()
- This returns the sign of a number.Math.log()
- This returns the natural logarithm of a number.Math.log2()
- This returns the base 2 logarithm of a number.Math.log10()
- This returns the base 10 logarithm of a number.Math.log1p()
- This returns the natural logarithm of 1 plus number.Math.exp()
- This returns e (Euler's number) raised to given power.Math.expm1()
- This returns e raised to given power minus 1.Math.sinh()
- This returns the hyperbolic sine of a number.Math.cosh()
- This returns the hyperbolic cosine of a number.Math.tanh()
- This returns the hyperbolic tangent of a number.Math.asinh()
- This returns the hyperbolic arcsine of a number.Math.acosh()
- This returns the hyperbolic arc-cosine of a number.Math.atanh()
- This returns the hyperbolic arctangent of a number.Math.atan2()
- This returns arctangent of the quotient of arguments.Math.fround()
- This returns the 32-bit float representation of number.Math.hypot()
- This returns sqrt of the sum of squares of arguments.Math.cbrt()
- This returns the cube root of a specified number.Math.clz32()
- This returns num of leading zeros in binary of a number.Math.random()
- This returns pseudo-random float number between 0 and 1.
Activity
Try It! Pick 5 of these methods and try them out in your console.