The Built-in Javascript Math Object
Useful methods of the Math object
Javascript provides a convenient built-in Math
object that has properties and methods that aid in performing mathematical calculations.
As explained by w3schools:
Unlike other global objects, the Math object has no constructor. Methods and properties are static.
All methods and properties (constants) can be used without creating a Math object first.
Properties
The Math
object’s properties include various constants. One example is π which can be accessed by writing Math.PI
.
const radius = 3;
const circumference = 2 * Math.PI * radius;
console.log(circumference)
// expected output: 18.8495....
Methods
I use the Math
object’s methods much more often than I use its properties. Let’s spend the rest of this article discussing some of the ones I find most useful.
Note: In general, the Math
object’s methods expect to receive a number as the argument. If the argument passed in isn’t a number and can’t be converted to one, it will return NaN
.
Math.sqrt(x)
This method takes in a number and returns its (positive) square root.
Math.sqrt()
expects the argument to be a positive number, so if the argument passed in is negative, it returns NaN
.
console.log(Math.sqrt(4))
// expected output: 2console.log(Math.sqrt("16"))
// expected output: 4console.log(Math.sqrt("five")
// expected output: NaNconsole.log(Math.sqrt(-4))
// expected output: NaN
Math.round(x)
This takes in a number and returns the value of the argument rounded to the nearest integer.
console.log(Math.round(4.5))
// expected output: 5console.log(Math.round(4.2))
// expected output: 4
Math.floor(x)
This takes in a number and returns the value of the argument rounded down to the nearest integer (ie. returns the largest integer less than or equal to the provided number).
console.log(Math.floor(6.001))
// expected output: 6console.log(Math.floor(6.999))
// expected output: 6
Math.ceil(x)
This is the opposite of Math.floor
. It takes in a number and returns the value of the argument rounded up to the nearest integer (ie. returns the smallest integer greater than or equal to the provided number).
console.log(Math.ceil(6.001))
// expected output: 7
console.log(Math.ceil(6.999))
// expected output: 7
Math.min(a, b, c, …)
This method takes in any number of arguments and returns the smallest of the provided arguments.
In order to find the minimum value in an array, you can use the spread operator in order to supply the array values as the arguments.
If any of the values supplied as arguments is not a number and can’t be converted to one, it returns NaN
.
console.log(Math.min(1, 3, 2));
// expected output: 1console.log(Math.min(-1, -3, -2));
// expected output: -3const array1 = [1, 3, 2];console.log(Math.min(...array1));
// expected output: 1console.log(Math.min(3, "five", 1))
// expected output: NaN
Math.max(a, b, c, …)
This is the opposite of Math.min()
. It returns the largest of the numbers given as input.
console.log(Math.max(1, 3, 2));
// expected output: 3console.log(Math.max(-1, -3, -2));
// expected output: -1const array1 = [1, 3, 2];console.log(Math.max(...array1));
// expected output: 3console.log(Math.max(3, "five", 1))
// expected output: NaN
Unlike Math.round()
, Math.floor()
, and Math.ceil()
, which will return NaN
if they are called without any arguments, interestingly, Math.max()
and Math.min()
can be called with no arguments.
If called without arguments, Math.max()
will return -Infinity
and Math.min()
will return Infinity
.
No, that’s not a typo. I didn’t accidentally mix up the order.
I know this seems counterintuitive, that’s why I brought it up.
As explained in the MDN docs, for Math.max()
:
-Infinity
is the initial comparant because almost every other value is bigger, that's why when no arguments are given, -Infinity
is returned.
The opposite is true for Math.min()
, Infinity
is the initial comparant since any other value passed in will be smaller.
Math.random()
This method doesn’t take in any arguments, and it returns a float that is a pseudo-random number in the left-closed right-open range [0, 1). (For those of us who can never remember what the notation means, it’s a number greater than or equal to 0 [inclusive] and up to, but not including 1 (exclusive). )
Math.random()
// expected output: 0.4594633007874416 (for example)
We can use additional methods from the Math
object to scale and shift the generated number to a range that suits our needs.
For example if I want to generate a random integer in the range [1, 5] (between 1 and 5 inclusive):
Math.floor(Math.random() * 5) + 1
// expected output: well it's random.... but will be in the set
// {1,2,3,4,5}
To explain step-by-step:
Math.random() * 5
will generate a float between 0 (inclusive) and 5 (exclusive).
2. We use Math.floor()
to round it down to the nearest whole number, so now it will be 0, 1, 2, 3, or 4.
3. Then we add 1
, so the number will be 1, 2, 3, 4 or 5
More generally, to generate a random integer in the range [a, b] (between a and b inclusive), we can use the following formula:
Math.floor(Math.random() * (b - a + 1)) + a
Math.random()
is the method that I use the most, but we can see that it works well in tandem with some other Math
object methods.
Note: You can find documentation on all the Math properties and methods (including method signatures and how different input values are handled) here.