JavaScript Math object

The Math object contains many utility programs related to mathematics. This tutorial will cover all

The Math object contains many utility programs related to mathematics.

It contains constants and functions.

constant

article description
Math.E constantE, The base of the natural logarithm (approximately ~2.71828)
Math.LN10 Constant representing the baseE (natural) logarithm of 10
Math.LN2 Constant representing the baseE (natural) logarithm of 2
Math.LOG10E A constant representing the logarithm to the base 10E
Math.LOG2E The constant representing the logarithm to the base 2E
Math.PI π constant (~3.14159)
Math.SQRT1_2 The constant that represents the reciprocal of the square root of 2
Math.SQRT2 The constant that represents the square root of 2

Features

All these functions are static. Mathematics cannot be instantiated.

Math.abs()

Returns the absolute value of a number

Math.abs(2.5) //2.5
Math.abs(-2.5) //2.5

Math.acos()

Returns the arc cosine of the operand

Operand must be between -1 and 1

Math.acos(0.8) //0.6435011087932843

Math.asin()

Returns the arc sine of the operand

Operand must be between -1 and 1

Math.asin(0.8) //0.9272952180016123

Math.atan()

Returns the arc tangent of the operand

Math.atan(30) //1.5374753309166493

Math.atan2()

Returns the arc tangent of its parameter quotient.

Math.atan2(30, 20) //0.982793723247329

Math.ceil()

rounding

Math.ceil(2.5) //3
Math.ceil(2) //2
Math.ceil(2.1) //3
Math.ceil(2.99999) //3

Math.cos()

Returns the cosine of the angle expressed in radiance

Math.cos(0) //1
Math.cos(Math.PI) //-1

Math.exp()

Returns the value of Math.E multiplied by the exponent passed as a parameter

Math.exp(1) //2.718281828459045
Math.exp(2) //7.38905609893065
Math.exp(5) //148.4131591025766

Math.floor()

rounding

Math.floor(2.5) //2
Math.floor(2) //2
Math.floor(2.1) //2
Math.floor(2.99999) //2

Math.log()

Back to baseE(Natural logarithm

Math.log(10) //2.302585092994046
Math.log(Math.E) //1

Math.max()

Returns the highest number in the passed number set

Math.max(1,2,3,4,5) //5
Math.max(1) //1

Math.min()

Return the smallest number in the passed number set

Math.max(1,2,3,4,5) //1
Math.max(1) //1

Math.pow()

Return the first parameter to the second parameter

Math.pow(1, 2) //1
Math.pow(2, 1) //2
Math.pow(2, 2) //4
Math.pow(2, 4) //16

Math.random()

Returns a pseudo-random number between 0.0 and 1.0

Math.random() //0.9318168241227056
Math.random() //0.35268950194094395

Math.round()

Round the number to the nearest whole number

Math.round(1.2) //1
Math.round(1.6) //2

Math.sin()

Calculate the sine of the angle expressed in radiance

Math.sin(0) //0
Math.sin(Math.PI) //1.2246467991473532e-16)

Math.sqrt()

Returns the square root of the argument

Math.sqrt(4) //2
Math.sqrt(16) //4
Math.sqrt(5) //2.23606797749979

Math.tan()

Calculate the tangent of the angle expressed in radiance

Math.tan(0) //0
Math.tan(Math.PI) //-1.2246467991473532e-16

Download mine for freeJavaScript beginner's manual


More js tutorials: