javascript
Math | JavaScript Wiki | Fandom
JavaScript Wiki
Advertisement

The Math object is an object that allows you to perform mathematical tasks on numbers. This object includes several mathematical methods. JavaScript Examples

abs[]

Data type function
Return type number
Parameter list x (number)
Standard ECMA-262 §15.8.2.1
Documentation Mozilla, Microsoft

Returns the absolute value of (that is, a positive number of the same magnitude as) x.

Math.abs(-1) // 1
Math.abs(2)  // 2
Math.abs(3)  // 3
Math.abs(-4) // 4

round[]

Data type function
Return type number
Parameter list x (number)
Standard ECMA-262 §15.8.2.15
Documentation Mozilla, Microsoft

Rounds the number to the nearest integer.

Math.round(12.3)    // 12
Math.round(12.4999) // 12
Math.round(12.5)    // 13
Math.round(12.6)    // 13

If you want to round a number to a certain precision, such as for display as currency, you may wish to see Number.toFixed instead.

PI[]

Data type function
Return type number
Parameter list none
Standard
Documentation Mozilla

Returns the number of pi (π).

Math.PI // 3.1415926
Advertisement