JavaScript Math Library: Beginners Tutorial with Examples

Unlock the potential of JavaScript Math Library: A beginner's guide with examples to elevate your coding with mathematical functions.

InstructorZach Gollwitzer
Last UpdatedMarch 29, 2024
Estimated Read Time2 minutes

The JavaScript Math Library

Even if you are not building Finance web applications, you are going to need to know a couple common functions from the JavaScript Math library.

Now I want to touch on a minor point (at least in our journey) before we get started. Take a look at the following code.

const myDate = new Date();
const year = myDate.getFullYear();

const negNum = -50;
const posNum = Math.abs(negNum);

Dates and the Math library are unrelated, but do you notice anything weird about the code above? I do. In the first code snippet, we are creating a Date using new Date() while in the second snippet, we are using the Math library as Math.abs().

Don't we need to do this???

const math = new Math();


NO, this is an incorrect way to use the Math library and if you try to run that code, you're going to get the following error message:

Uncaught TypeError: Math is not a constructor

What is that word, "constructor"??

Well, it has to do with constructing an Object in JavaScript, and has its roots in something called "Object Oriented Programming" (OOP). Later in this series, we will discuss this along with the concept of "classes", "static methods", and "instance methods".

Math.abs() is a static method while myDate.getFullYear() is considered an instance method. This is not necessary to know right now, but I wanted to point it out so that when you see it in the future, it is not a complete surprise.

Some common uses of the Math library

Even for beginners, the documentation for the Math library is not that difficult to read.

Here are some common ways (not exhaustive) to use it:

// Math has some built-in "constants" you can use
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045

// And some static methods
// Takes absolute value of number
Math.abs(-60); // 60

// Rounds up to nearest integer
Math.ceil(Math.PI); // 4

// Rounds down to the nearest integer
Math.floor(Math.PI); // 3

// Rounds to nearest integer
Math.round(Math.PI); // 3

// Returns smallest/largest number
Math.min(3, 4, 5, 6); // 3
Math.max(3, 4, 5, 6); // 6

// Returns a random number between 0 and 1

Before we move on from this, I want to focus on that Math.random() method a little bit longer because it will come in handy if you know how to use it.

Since it returns a random value between 0 and 1, we can use this fact along with some basic logic to get a random index in an array. We have used this in previous lessons of this series but I have not yet explained how it works.

// Gives us a random number between 0 and 1
const randomNumber = Math.random();

// By multiplying by 100, we move the decimal over 2 spaces, and now, we have
// a number between 0 and 100 (but it is still a decimal)
const largerNumber = randomNumber * 100;

// By rounding down, we now have a random, whole number from 0-99
const wholeNumber = Math.floor(largerNumber);

// Now let's see how this can be useful
const arr = ["just", "an", "example", "array"];
const lengthOfArr = arr.length; // 4

// Let's combine everything together
// This gives us a random, whole number from 0 - 3, which is the same
// index values we need to access values of our array
const randomIndexForArray = Math.floor(Math.random() * lengthOfArr);

const randomArrValue = arr[randomIndexForArray];

You may not use this trick in many of your applications, but it sure is useful for unit testing!