Tutorials Tonight
Follow Us

JavaScript Math And Its Functions


In this tutorial, you will learn about Math object in JavaScript. You will learn about all the functions available in the Math object with examples.

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

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 without creating objects. example Math.PI, Math.sin(x) etc.

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

javascript math

Example

// get PI value
var pi = Math.PI;
console.log(pi);

// get value of sin
var sine = Math.sin(0.5);
console.log(sine);
▶ Try it

The Math object has many properties and methods which is used to get values of mathematical constants and functions like E, Log2, PI, sin, cos, etc.

Let's see the list of all the properties and methods of the Math object.

Math Object Properties

The following table lists all the properties of the Math object.

Property Description
Math.PI Returns the value of mathematical constant PI (ratio of circumference to diameter)
Math.E Returns Euler's constant (base of natural logarithm)
Math.LN2 Returns the value of the natural logarithm of 2 (approx 0.693)
Math.LN10 Returns the value of the natural logarithm of 10 (approx 2.302)
Math.LOG2E Returns the value of base 2 logarithm of e (approx 1.443)
Math.LOG10E Returns the value of base 10 logarithm of e (approx 0.434)
Math.SQRT1_2 Returns the value of the square root of 1/2 (approx 0.707)
Math.SQRT2 Returns the value of the square root of 2 (approx 1.414)

Math.PI

PI is a static property of the Math object. It returns the value of mathematical PI (π).

You can use π to calculate the area of the circle.

Example

// value of pi
var pi = Math.PI; // 3.141592653589793
console.log(pi);

// area of circle
var radius = 5;
var area = pi * radius * radius;
console.log(area);
▶ Try it

Math.E

E is a static property of the Math object. It returns the value of mathematical constant E (2.718281828459045). Used as Math.E.

Value of E is used in logarithm function, probability, etc.

Example

// value of e
var e = Math.E; // 2.718281828459045
console.log(e);
▶ Try it

Math.LN2

LN2 is a static property of a Math object. It returns the value of the natural logarithm of 2 (approx 0.693).

Example

// value of natural logarithm of 2
var ln2 = Math.LN2; // 0.6931471805599453
console.log(ln2);
▶ Try it

Math.LN10

Similar to LN2, LN10 is a static property of a Math object. It returns the value of natural logarithm of 10 (approx 2.302).

Example

// value of natural logarithm of 10
var ln10 = Math.LN10; // 2.302585092994046
console.log(ln10);
▶ Try it

Math.LOG2E

LOG2E is also a static property of a Math object. It returns the value of base 2 logarithm of e (approx 1.443).

Example

// value of base 2 logarithm of e
var log2e = Math.LOG2E; // 1.4426950408889634
console.log(log2e);
▶ Try it

Math.LOG10E

LOG10E means base 10 logarithm of e (log10e). It is a static property of Math object. It returns a value approx 0.434.

Example

// value of base 10 logarithm of e
var log10e = Math.LOG10E; // 0.4342944819032518
console.log(log10e);
▶ Try it

Math.SQRT1_2

To get the value of the square root of 1/2 (√½), use the SQRT1_2 property of the Math object. It returns a value of approx 0.707.

Example

// value of square root of 1/2
var sqroot = Math.SQRT1_2; // 0.7071067811865476
console.log(sqroot);
▶ Try it

Math.SQRT2

To get the value of square root of 2 (√2), use SQRT2 property of Math object. It returns a value approx 1.414.

Example

// value of square root of 2
var sqroot = Math.SQRT2; // 1.4142135623730951
console.log(sqroot);
▶ Try it

Javascript Math methods

There are many methods available in Math object. They are as follows:

Method Description
Cosine Functions
Math.cos(x) Returns the cosine of x
Math.cosh(x) Returns the hyperbolic cosine of x
Math.acos() Returns the arccosine of a given number in radian
Math.acosh() Returns the hyperbolic arccosine of a number
Sine Functions
Math.sin(x) Returns the sine of x
Math.sinh(x) Returns the hyperbolic sine of x
Math.asine() Returns the arcsine of given number in radian
Math.asinh() Returns the hyperbolic arccosine of a number
Tangent Functions
Math.tan() Returns the tangent of given number in radian
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.tanh() Returns the hyperbolic tangent of a number
Min/Max Functions
Math.min() Returns the smallest of the given numbers
Math.max() Returns the largest of the given numbers
Upper/Lower Bound Functions
Math.ceil(x) Returns the smallest integer greater than or equal to x
Math.floor(x) Returns the largest integer less than or equal to x
Logarithemic Functions
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.exp(x) Returns the Ex, where E is Euler number
Misscelenious Functions
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.abs(x) Returns the absolute value of x
Math.sign(x) Returns sign of x, +ve -ve or 0
Math.sqrt(x) Returns the square root of x
Math.cbrt(x) Returns the cube root of x

JavaScript sin functions

Sine functions in javascript are used to get the value of different sin trigonometric functions like sin, asin, sinh, and asinh

Here is an example of each function with its description.

Example

// sin function x/y value
console.log(Math.sin(0));
console.log(Math.sin(Math.PI/2));

// asin function
console.log(Math.asin(1));
console.log(Math.asin(0.5));

// sinh function
console.log(Math.sinh(0));
console.log(Math.sinh(1));

// asinh function
console.log(Math.asinh(1));
console.log(Math.asinh(0.5));
▶ Try it

JavaScript cos functions

Cosine functions in javascript are used to get the value of different cos trigonometric functions like cos, acos, cosh, and acosh

Here is example of each cos function with its description.

Example

// cos function
console.log(Math.cos(0));
console.log(Math.cos(Math.PI/2));

// acos function
console.log(Math.acos(1));
console.log(Math.acos(0.5));

// cosh function
console.log(Math.cosh(0));
console.log(Math.cosh(1));

// acosh function
console.log(Math.acosh(1));
console.log(Math.acosh(0.5));
▶ Try it

JavaScript tan functions

Tan functions in javascript are used to get value of different tan trigonometric functions like tan, atan, atan2, tanh, and atanh

Here is example of each tan function with its description.

Example

// tan function
console.log(Math.tan(0));
console.log(Math.tan(Math.PI/2));

// atan function
console.log(Math.atan(1));
console.log(Math.atan(0.5));

// atan2 function
console.log(Math.atan2(1,1));
console.log(Math.atan2(0.5,0.5));

// tanh function
console.log(Math.tanh(0));
console.log(Math.tanh(1));

// atanh function
console.log(Math.atanh(1));
console.log(Math.atanh(0.5));
▶ Try it

JavaScript min max functions

Min and max functions in javascript are used to get the minimum and maximum value of two numbers or more numbers.

Here is an example of each min and max function with its description.

Example

console.log(Math.min(34,4));
console.log(Math.max(34,4));

var arr = [1,2,3,4,5,6,7,8,9,10];
console.log(Math.min(...arr));
console.log(Math.max(...arr));
▶ Try it

JavaScript log functions

Log functions in javascript are used to get value of different log trigonometric functions like log, log10, log2, and exp.

Here is an example of each log function with its description.

Example

// log function
console.log(Math.log(1));
console.log(Math.log(10));

// log10 function
console.log(Math.log10(1));
console.log(Math.log10(10));

// log2 function
console.log(Math.log2(1));
console.log(Math.log2(10));

// exp function
console.log(Math.exp(1));
console.log(Math.exp(10));
▶ Try it

JavaScript abs method

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

Example

console.log(Math.abs(-5)); // 5
console.log(Math.abs(0)); // 0
console.log(Math.abs(-2.5)); // 2.5
console.log(Math.abs({})); // NaN
console.log(Math.abs(null)); // 0
console.log(Math.abs(undefined)); // NaN
console.log(Math.abs(Infinity)); // Infinity
console.log(Math.abs([12])); // 12
console.log(Math.abs([2,4])); // NaN
▶ Try it

JavaScript ceil and floor Method

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

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

Example

console.log(Math.floor(8.5243));
console.log(Math.floor(-34.67));
console.log(Math.floor(5.85));
console.log(Math.floor(Math.PI));

console.log(Math.ceil(8.5243));
console.log(Math.ceil(-34.67));
console.log(Math.ceil(5.85));
console.log(Math.ceil(Math.PI));
▶ Try it

JavaScript random function

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

console.log(Math.random()); // random number between 0 - 1
console.log(Math.random()*10); // random number between 0 - 10
console.log(Math.floor(Math.random() * 15)); // Integer random number between 0 - 15
▶ Try it

JavaScript round method

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

Example

console.log(Math.round(8.5243));
console.log(Math.round(-8.5));
console.log(Math.round(2.325));
console.log(Math.round(-2.5));
▶ Try it

Conclusion

JavaScript Math object gives us many useful mathematical functions. We can use these functions to calculate the value of any mathematical expression.

In this article, we have covered all the properties and methods of Math object in javascript.


Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.