The JavaScript Math object permits you to perform specific mathematical operations. It is one of JavaScript built-in objects, like Date and Number. The Math object has a number of specific methods for determining things like the rounded value of a number, a random number, the highest or lowest number in a selection of numbers, the square root of a number and so on.

Math is a JavaScript built-in object, but it is not a function object.

The result of the math functions are dependant on how they are implemented. This means that the result may vary slightly depending on the way the operating system does the calculations.

 

Common uses of the JavaScript Math object

There are many built in functions for the JavaScript Math object. Below are some examples of the more commonly used functions.


Math.abs(x)

This function returns the absolute value of a number. If the original is number is positive or negative, the result is a positive number. If the number has fractional digits, the result will have fractional digits. If the parameter is null, it will return 0. If the parameter is a string, it will return NaN, which can be useful for validating forms. If the parameter is a Boolean true, it returns 1. If the parameter is a Boolean false, it returns 0.

Examples:

var x = -12.3459;
var absValueofx = Math.abs(x); // the value of absValueofx is 12.3459
var y = "The rain in Spain";
var absValueofy = Math.abs(y); // the value of absValueofy is NaN
var z = null;
var absValueofz = Math.abs(z); // the value of absValueofz is 0
var w = true;
var absValueofw = Math.abs(w); // the value of absValueofw is 1

Math.floor(x)

This function will  return the largest integer less than or equal to the number in the parentheses.  It rounds down to the next integer.  

Examples:

var x = 25.678;
var resultVal = Math.floor(x); // this will return 25
var y = -24.678;
var resultVal = Math.floor(y); // this will return -24 
var z = 25.22;
var resultVal = Math.floor(z); // this will return 25


Math.ceil(x)

This function returns the smallest integer greater than or equal to the number in the parentheses. It rounds up to the next integer

var x = 25.678;
var resultVal = Math.ceil(x); // this will return 25
var y = -24.678;
var resultVal = Math.ceil(y); // this will return -24 
var z = 25.22;
var resultVal = Math.ceil(z); // this will return 26


Math.round(x)

This function returns the value of the integer closest to the number in parentheses. It rounds up or down to the next integer.

var x = 25.678;
var resultVal = Math.round(x); // this will return 26
var y = -24.678;
var resultVal = Math.round(y); // this will return -25
var z = 25.22;
var resultVal = Math.round(z); // this will return 25


Math.random()

This function returns a floating point, pseudo-random number between 0 up to 1, but not including 1.  To generate random integers, you then do other calculations for multiplying and removing decimal places. The parentheses do not take any arguments.

var calcValue = Math.random();
var randInt = Math.floor(calcValue * 100); // returns a random integer between 0 up to 100, but not including 100

 

List of JavaScript Math Object Functions

There are many JavaScript Math object functions for performing a variety of mathematical operations, including trigonometry.

Math.abs(x) returns the absolute value of a number
Math.acos(x)  returns the arcosine of a number
Math.acosh(x) returns the hyperbolic arccosine of a number
Math.asin(x) returns the arcsine of a number
Math.asinh(x) returns the hyperbolic arcsine of a number
Math.atan(x) returns the arctangent of a number
Math.atanh(x) returns the hyperbolic arctangent of a number
Math.atan2(x,y) returns the arctangent of the quotient of its arguments
Math.cbrt(x) returns the cube root of a number
Math.ceil(x) returns the smallest integer greater than or equal to a number
Math.clz32(x) returns the number of leading zeros of a 32-bit integer
Math.cos(x) returns the cosine of a number
Math.cosh(x) returns the hyperbolic cosine of a number
Math.exp(x) returns Ex, where x is the argument and E is Euler's constant (2.718...) which is the base of the natural logarithm
Math.expm1(x) returns subtracting 1 for exp(x)
Math.floor(x) returns the largest integer less than or equal to a number
Math.fround(x) returns the nearest single precision float representation of a number
Math.hypot([x[,y[,...]]]) returns the square root of the sum of squares of the arguments
Math.imul(x) returns the result of a 32-bit integer multiplication
Math.log1p(x) returns the natural logarithm of a number (loge or ln)
Math.log10(x) returns the base 10 logarithm of a number
Math.log2(x) returns the base 2 logarithm of a number
Math.max([x[,y[,...]]]) returns the largest of zero or more numbers
Math.min([x[,y[,...]]]) returns the smallest of zero or more numbers
Math.pow(x, y) returns base to the exponent power
Math.random() returns a pseudo-random number within the range of 0 to 1
Math.round(x) returns the value of a number rounded to the nearest integer
Math.sin(x) returns the sine of a number
Math.sinh(x) returns the hyperbolic sine of a number
Math.sqrt(x) returns the square root of a number
Math.tan(x) returns the tangent of a number
Math.tanh(x) returns the hyperbolic tangent of a number
Math.trunc(x) returns the integral part of a number, removing any fractional digits