DEV Community 👩‍💻👨‍💻 is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Hassib Moddasser

Posted on • Updated on • Originally published at hassib.hashnode.dev

Ten+ Math Functions Every JavaScript Developer Should Know

In this article, I want to show you the commonly used Math functions that every JavaScript developer should know. In addition to general examples, I have provided different use-case examples to clarify the context of functions.

Let's dive in and have fun!

Introduction

Math is a built-in object that provides properties and methods for mathematical constants and functions to execute mathematical operations. Unlike many other global objects, the Math object has no constructor. All properties and methods of Math are static.

The Math works with the Number type. It doesn't work with BigInt.

Following is a list of commonly used Math object methods:

`Math.abs`

The `Math.abs` function returns the absolute value of a number. It takes a number as its parameter and returns its absolute value.

``````console.log(Math.abs(5));
// Output: 5

console.log(Math.abs(5.2));
// Output: 5.2

console.log(Math.abs(-5.2));
// Output: 5.2
``````

Bonus Tip: It is useful when you have a case where you should find the difference between the two numbers. Let's take a look at the example below:

``````function difference(a, b) {
return Math.abs(a - b);
}

console.log(difference(4, 7));
// Output: 3

console.log(difference(7, 4));
// Output: 3
``````

`Math.ceil`

The `Math.ceil` function always rounds a number up to the next largest integer. It takes a number as its parameter and returns its upward round value.

``````console.log(Math.ceil(0.8));
// Output: 1

console.log(Math.ceil(-0.8));
// Output: -0

console.log(Math.ceil(4));
// Output: 4

console.log(Math.ceil(3.004));
// Output: 4

console.log(Math.ceil(-3.004));
// Output: -3
``````

`Math.floor`

The `Math.floor` function rounds a number down to its nearest integer. It takes a number as its parameter and returns its downward round value.

``````console.log(Math.floor(0.8));
// Output: 0

console.log(Math.floor(-0.8));
// Output: -1

console.log(Math.floor(4));
// Output: 4

console.log(Math.floor(3.004));
// Output: 3

console.log(Math.floor(-3.004));
// Output: -4
``````

`Math.round`

The `Math.round` function rounds a number to its nearest integer. It takes a number as its parameter and returns its nearest rounded value.

``````console.log(Math.round(0.8));
// Output: 1

console.log(Math.round(-0.8));
// Output: -1

console.log(Math.round(5));
// Output: 5

console.log(Math.round(5.95));
// Output: 6

console.log(Math.round(5.5));
// Output: 6

console.log(Math.round(5.05));
// Output: 5
``````

Description: If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞.

Consider the following examples for better clarity:

``````console.log(Math.round(10.49));
// Output: 10

console.log(Math.round(10.5));
// Output: 11

console.log(Math.round(22));
// Output: 22

console.log(Math.round(-10.5));
// Output: -10

console.log(Math.round(-10.51));
// Output: -11
``````

`Math.trunc`

The `Math.trunc` function takes a number as its parameter and returns its integer part of a number by removing any fractional digits.

``````console.log(Math.trunc(0.8));
// Output: 0

console.log(Math.trunc(-0.8));
// Output: -0

console.log(Math.trunc(5));
// Output: 5

console.log(Math.trunc(5.95));
// Output: 5

console.log(Math.trunc(5.5));
// Output: 5

console.log(Math.trunc(5.05));
// Output: 5
``````

Description: Unlike the other Math methods, the way `Math.trunc` works is very simple. It truncates (cuts off) the dot and the digits to the right of it, no matter whether the argument is a positive or negative number.

The `Math.trunc` is one of the new features of ECMAScript 6 and before that, the following method was used:

``````// In ECMAScript 5
function mathTrunc(num) {
return (num < 0 ? Math.ceil(num) : Math.floor(num));
}
``````

`Math.max`

The `Math.max` function returns the largest of `0` to `n` numbers given as input parameters.

``````let largestNumber = Math.max(2, 5, 15, 3);

console.log(largestNumber);
// Output: 15
``````

`Math.min`

The `Math.min` function returns the smallest of `0` to `n` numbers given as input parameters.

``````let smallestNumber = Math.min(2, 5, 15, 3);

console.log(smallestNumber );
// Output: 2
``````

Note: Both `Math.max` & `Math.min` are variadic functions. A variadic function takes a variable number of arguments. In other words, a variadic function is a function where the total number of parameters are unknown and can be adjusted when the method is invoked.

Bonus Tip: You can also find the largest/smallest number in an array with the help of the Spread Syntax, which unpacks array values. Take a look at the example below:

``````let salaries = [1000, 2500, 400, 14000, 800];

let highestSalary = Math.max(...salaries);
console.log(highestSalary);
// Output: 14000

let lowestSalary = Math.min(...salaries);
console.log(lowestSalary);
// Output: 400
``````

`Math.pow`

The `Math.pow` function returns the `base` to the `exponent` power. It takes two number parameters and returns as `base^exponent`.

``````console.log(Math.pow(2, 5));
// Output: 32

console.log(Math.pow(2, 0));
// Output: 1
``````

`Math.sqrt`

The `Math.sqrt` function takes a number as a parameter and returns the square root of the number.

``````console.log(Math.sqrt(9));
// Output: 3

console.log(Math.sqrt(2));
// Output: 1.4142135623730951

console.log(Math.sqrt(-9));
// Output: NaN
``````

`Math.cbrt`

The `Math.cbrt` function returns the cube root of a number taken as a parameter.

``````console.log(Math.cbrt(64));
// Output: 4

console.log(Math.cbrt(-1));
// Output: -1

console.log(Math.cbrt(1));
// Output: 1
``````

`Math.random`

The `Math.random` function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, and exclusive of 1).

``````console.log(Math.random());
// Output: 0.9785027066546665

console.log(Math.random());
// Output: 0.4401509062770659

console.log(Math.random());
// Output: 0.04055758334158077
``````

You can also generate random integer numbers, one can pass the `Math.random()` as an argument to the `parseInt` method.

``````let randomNumber = parseInt((Math.random() * 100), 10);

console.log(randomNumber);
// Output: 80
``````

In the above code, the `Math.random` function returns a random integer between 0 and 1. To convert that to a random number up to 100, multiply it by 100. So if you want a random number up to 10, multiply it by 10. The `parseInt` function casts the generated random number to an integer by the radix of 10, which indicates to convert from a decimal number.

Bonus Tip: To get a random integer within a specific range you can use the following method instead:

``````function generateRandomNumber(min, max) {
return parseInt((Math.random() * (max - min + 1)), 10) + min;
}

console.log(generateRandomNumber(20, 50));
// Output: 32

console.log(generateRandomNumber(80, 100));
// Output: 89
``````

Final thoughts

The `Math` object allows you to perform mathematical instructions. It has 8 static properties and more than 30 static methods.

Let's look at a glance at what common Math functions are:

Function Description
Math.abs(x) Returns the absolute value of a number.
Math.ceil(x) Rounds a number up to the next largest integer.
Math.floor(x) Rounds a number down to its nearest integer.
Math.round(x) Rounds a number to its nearest integer.
Math.trunc(x) Returns the integer part of a number.
Math.max(n1, n2, n3, ..., nx) Returns the number with the highest value.
Math.min(n1, n2, n3, ..., nx) Returns the number with the lowest value.
Math.pow(x, y) Returns the base to the exponent power.
Math.sqrt(x) Returns the square root of a number.
Math.cbrt(x) Returns the cubic root of a number.
Math.random() Returns a random number between 0 (inclusive) and 1 (exclusive).