tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT MATH



In javascript, Math is a built-in object which has many properties and methods that is used to deal with mathematical constants and functions.

Math is not a constructor, Unlike Array or String. All the methods and properties of Math are static, meaning you can directly use methods and properties with class names. example Math.PI, Math.sin(x) etc.

Note: Static methods and properties belong to class not to its object. All the methods and properties can be called on Math as an object.

Example

Run the code

Math Object Properties

Property Description
Math.PI returns value of pi (ratio of circumference to diameter)
Math.E returns euler's constant
Math.LN2 returns natural logarithm of 2 (approx 0.693)
Math.LN10 returns natural logarithm of 10 (approx 2.303)
Math.LOG2E returns logarithm of E at base 2 (approx 1.443)
Math.LOG10E returns logarithm of E at base 10 (approx 0.434)
Math.SQRT1_2 returns Square root of 1/2 (approx 0.707)
Math.SQRT2 returns Square root of 2 (approx 1.414)

Math.PI

Example: Math.PI

Run the code

Math.E

Example: Math.E

Run the code

Math.LN2

Example: Math.LN2

Run the code

Math.LN10

Example: Math.LN10

Run the code

Math.LOG2E

Example: Math.LOG2E

Run the code

Math.LOG10E

Example: Math.LOG10E

Run the code

Math.SQRT1_2

Example: Math.SQRT1_2

Run the code

Math.SQRT2

Example: Math.SQRT2

Run the code

Javascript Math methods

Method Description
Math.abs(x) Returns the absolute value of x
Math.acos() Returns the arccosine of given number in radian
Math.acosh() Returns the hyperbolic arccosine of a number
Math.asine() Returns the arcsine of given number in radian
Math.asinh() Returns the hyperbolic arccosine of a number
Math.atan() Returns the arctangent of given number in radian
Math.atan2(y,x) Returns the arctangent of quotient of its argument
Math.atanh(y,x) Returns the hyperbolic arctangent of x
Math.cbrt(x) Returns the cube root of x
Math.ceil(x) Returns the smallest integer greater than or equal to x
Math.cos(x) Returns the cosine of x
Math.cosh(x) Returns the hyperbolic cosine of x
Math.exp(x) Returns the Ex, where E is Euler number
Math.floor(x) Returns the largest integer less than or equal to x
Math.log(x) Returns the natural logarithm of x (loge )
Math.log10(x) Returns the base-10 logarithm of x
Math.log2(x) Returns the base-2 logarithm of x
Math.pow(x,y) Returns base x to exponent y ( xy )
Math.random() Returns a random number between 0 and 1
Math.round(x) Returns the value of number rounded to nearest integer
Math.sign(x) Returns sign of x, +ve -ve or 0
Math.sqrt(x) Returns the square root of x

Lets see few Examples.

The abs() method

The Math.abs(x) method return absolute value of x.

Example: Math.abs(x)

Run the code

The Math.floor(x) and Math.ceil(x) method

The Math.floor(x) method return largest number less than or equal to x.

The Math.ceil(x) method return smallest number greater than or equal to x.

Example

Run the code

The random() method

The Math.random() method returns a random number in between 0 and 1.

This method can be used to get a random number between any integer.

Example: Math.random()

Run the code

The round(x) method

The Math.round(x) method returns the value of the round number nearest to x.

Example: Math.round(x)

Run the code

Trigonometric operation in Javascript

The Math object has many built-in trigonometric methods that can be used to calculate Trigonometric values.

Example: Math.trigonometry

Run the code