Skip to content

Math

Overview

Mathematical operations. It includes mathematical functions such as Math.abs, Math.pow, Math.ceil, Math.random, Math.sqrt, and Math.round

abs

Returns the absolute value of x

javascript
Math.abs(x)
ParameterTypeDescription
xInteger or DoubleNumber to calculate absolute value

Return Value

Type Integer or Double Absolute value of number x

Examples

javascript
Math.abs(-100.3) //100.3

pow

Returns d1 raised to the power of d2

javascript
Math.pow(d1, d2)
ParameterTypeDescription
d1Integer or DoubleNumber to calculate the power of
d2Integer or DoubleExponent

Return Value

Type Double Number d1 raised to the power of d2

Examples

javascript
Math.pow(2, 3) //8.0

ceil

Returns the smallest integer greater than or equal to x

javascript
Math.ceil(x)
ParameterTypeDescription
xDoubleNumber to compare

Return Value

Type Double The smallest integer greater than or equal to x

TIP

  • Note that the return type of the function is a decimal

Examples

javascript
Math.ceil(2.2) //3.0

floor

Returns the largest integer less than or equal to x

javascript
Math.floor(x)
ParameterTypeDescription
xDoubleNumber to compare

Return Value

Type Double The largest integer less than or equal to x

TIP

  • Note that the return type of the function is a decimal

Examples

javascript
Math.floor(2.2) //2

random

Returns a random number between 0 and 1

javascript
Math.random()

Return Value

Type Double A decimal greater than or equal to 0 and less than 1, returns a random number each time it is called

Examples

javascript
Math.random() //0.6260832016946124

sqrt

Returns the square root of x

javascript
Math.sqrt(x)
ParameterTypeDescription
xDoubleNumber to calculate the square root of

Return Value

Type Double Square root of x

Examples

javascript
Math.sqrt(4) //2.0

round

Returns the value of number n rounded to the nearest number with digits decimal places

javascript
Math.round(n, digits)
ParameterTypeDescription
nDoubleNumber to round
digitsIntegerNumber of decimal places for precision

Return Value

Type Double Rounded value

Examples

javascript
Math.round(3.1415926, 2)//3.14
Math.round(3.1415926, 4)//3.1416