The Math Object

Javascript (and some other languages) have built-in mathematics commands to simplify your life. In JS this is called the Math object.

The various abilities of the Math object can be found in various locations online. Here is a sampling of what you might use.

Code

Description

Math.PI

Returns 3.141592653589793

Math.pow(base, exponent)

Returns the base raised to the exponent

Math.sqrt(number)

Returns the square root of the given number

Math.round(number)

Rounds the given number to the nearest integer

Math.ceil(number)

Returns the number rounded up to the nearest integer

Ex. Math.ceil(5.1) returns 6

Math.floor(number)

Returns the number rounded down to the nearest integer

Ex. Math.floor(5.8) returns 5

Math.random()

Returns a pseudorandom decimal number between 0 and 1. Use it in conjunction with Math.floor() to return an integer.

Last updated