Javascript Math - 26 Methods (Reference)

Tech:
Javascript
Since:
2 years ago
Views:
2

In Javascript Math Object provides a collection of methods that can be used to perform mathematical operations.

abs()

The abs() method returns the absolute value of a number.

Math.abs(-7.25);
// 7.25

// non-negative number

trunc()

The trunc() method returns the integer part of a number by removing any fractional digits.

Math.trunc(8.76);
// 8

// Integer part

sqrt()

The sqrt() method returns the square root of a number.

Math.sqrt(9);
// 3

// square root

cbrt()

The cbrt() method returns the cube root of a number.

Math.cbrt(125);
// 5

// cubic root

ceil()

The ceil() method returns the smallest integer greater than or equal to a number.

Math.ceil(1.4);
// 2

// rounded up

floor()

The floor() method returns the largest integer less than or equal to a number.

Math.floor(1.6);
// 1

// rounded down

round()

The round() method returns the value of a number rounded to the nearest integer.

Math.round(2.5);
// 3

// rounded up or down

exp()

The exp() method returns the value of E^x.

Math.exp(1);
// 2.718281828459045

// Ex, where E is Euler's number

log()

The log() method returns the natural logarithm (base E) of a number.

Math.log(2);
// 0.6931471805599453

// natural log

max()

The max() method returns the largest of zero or more numbers.

Math.max(5, 10);
// 10

// highest value

min()

The min() method returns the smallest of zero or more numbers.

Math.min(5, 10);
// 5

// lowest value

pow()

The pow() method returns the base to the exponent power, that is, base^exponent.

Math.pow(4, 3);
// 64

// x to the power of y
// 43

random()

The random() method returns a floating-point, pseudo-random number in the range [0, 1).

Math.random();
// 0.24698129052142104

// between 0 >= and < 1

cos()

The cos() method returns the cosine of a number.

Math.cos(3);
// -0.9899924966004454

// cosine

cosh()

The cosh() method returns the hyperbolic cosine of a number.

Math.cosh(3);
// 10.067661995777765

// hyperbolic cosine

acos()

The acos() method returns the arccosine (in radians) of a number.

Math.acos(0.5);
// 1.0471975511965979

// arccosine

acosh()

The acosh() method returns the hyperbolic arccosine (in radians) of a number.

Math.acosh(2);
// 1.3169578969248166

// hyperbolic arccosine

sin()

The sin() method returns the sine of a number.

Math.sin(3);
// 0.1411200080598672

// sine

sinh()

The sinh() method returns the hyperbolic sine of a number.

Math.sinh(3);
// 10.017874927409903

// hyperbolic sine

asin()

The asin() method returns the arcsine (in radians) of a number.

Math.asin(0.5);
// 0.5235987755982989

// arcsine

asinh()

The asinh() method returns the hyperbolic arcsine (in radians) of a number.

Math.asinh(1);
// 0.881373587019543

// hyperbolic arcsine

tan()

The tan() method returns the tangent of a number.

Math.tan(1);
// 1.5574077246549023

// tangent

tanh()

The tanh() method returns the hyperbolic tangent of a number.

Math.tanh(1);
// 0.7615941559557649

// hyperbolic tangent

atan()

The atan() method returns the arctangent (in radians) of a number.

Math.atan(2);
// 1.1071487177940904

// arctangent

atanh()

The atanh() method returns the hyperbolic arctangent (in radians) of a number.

Math.atanh(0.5);
// 0.5493061443340548

// hyperbolic arctangent

atan2()

The atan2() method returns the arctangent (in radians) of the quotient of its arguments.

Math.atan2(8, 4);
// 1.1071487177940904

// arctangent of 8/4