5 minutes read

In JavaScript, in addition to objects that we can create using the syntax of the language, there are several built-in objects such as Date, Math, Number, Function, and others. All of them have their own properties and methods that we can use in our needs. One of the most important and frequently used built-in objects is Math.

The Math object

With Math, we can round a number, raise a number to a power, find the largest or smallest number, and much more. Without it, we would have to write rather verbose functions for all these calculations ourselves, while it allows us to perform calculations of varying complexity with just one line of code. Take a look at the example below — does it make sense to create a large and complex function when you can just use the Math object?

Compare this and complex function:

function pow(x, n) {
  let result = 1;

  for (let i = 0; i < n; i++) {
    result = result * x;
  }

  return result;
}

pow(2, 3); // 8

...with this, which gets absolutely the same result, only using the Math object:

Math.pow(2, 3); // 8

The Math object, unlike objects like Date, Function, or Error, is not a constructor. All properties and methods of the Math object are static. This means we don't have to instantiate Math for later use.

// using the Date object

const date = new Date()
date.now()

// using the Math object

Math.round(4.6)

Math is an object that stores more than 40 mathematical constants and functions in its properties and methods. Some of them are used very rarely; in fact, now we will learn only about the most widely used methods of the Math object and analyze how Math can be used in practice.

Methods of the Math object

1. Rounding Math methods

The Math object contains four methods for rounding numbers:

  • Math.round(x) – returns x rounded to the nearest integer (if the fractional part of x is half, it will round up):
Math.round(3.9); // 4
Math.round(3.5); // 4
Math.round(3.3); // 3
Math.round(-3.2); // -3
Math.round(-3.7); // -4
  • Math.ceil(x) – returns x rounded up to the nearest whole number:
Math.ceil(3.9); // 4
Math.ceil(3.5); // 4
Math.ceil(3.3); // 4
Math.ceil(-3.2); // -3
Math.ceil(-3.7); // -3
  • Math.floor(x) – returns x rounded down to the nearest whole number:
Math.floor(3.9); // 3
Math.floor(3.5); // 3
Math.floor(3.3); // 3
Math.floor(-3.2); // -4
Math.floor(-3.7); // -4
  • Math.trunc(x) – returns the integer part of x:
Math.trunc(3.9); // 3
Math.trunc(3.5); // 3
Math.trunc(3.3); // 3
Math.trunc(-3.2); // -3
Math.trunc(-3.7); // -3

With the help of Math.round(), Math.ceil(), Math.floor(), and Math.trunc() we can get not only integers but also fraction numbers if we need them. So, we can round the number to tenths, hundredths, thousandths, and so on:

Math.round(3.753 * 10) / 10; // 3.8

Math.ceil(3.237 * 10) / 10; // 3.3

Math.floor(3.753 * 100) / 100; // 3.75

Math.trunc(3.999 * 100) / 100; // 3.99

2. Other methods of the Math object

  • Math.sign(x) – returns 1 if x > 0, returns 0 if x === 0, and -1 if x < 0
Math.sign(55); // 1
Math.sign(0); // 0
Math.sign(-33.3); // -1
  • Math.pow(x, y) – returns x raised to the power of y;
Math.pow(2, 3); // 8
Math.pow(7, 3); // 343
  • Math.sqrt(x) – returns the square root of x;
Math.sqrt(9); // 3
Math.sqrt(49); // 7
  • Math.abs(x) – returns the absolute value of x. Absolute value means that it returns a positive number ;
Math.abs(10); // 10
Math.abs(0); // 0
Math.abs(-5); // 5

3. Min and max methods of the Math.object()

  • Math.min(x,y,z) – returns the smaller number of them specified in the arguments:
Math.min(10, 5, 2); // 2
Math.min(-2,3,4); // -2
  • Math.max(x,y,z) –returns the maximum number of them specified in the arguments:
Math.max(10, 5, 2); // 10
Math.max(-2,3,4); // 4

4. Random method of the Math.object()

Math.random() – returns a random number from 0 (inclusive) to 1 (not inclusive):

Math.random(); // 0.4314660299261446
Math.random(); // 0.3985324055507067
Math.random(); // 0.9646181780133591

By multiplying the number returned using Math.random(), we can get not just a number from 0 to 1, but also in a different interval. In the example below, we use the Math.random() and Math.round() methods to get an integer from 0 (inclusive) to 100 (not inclusive);

Math.round(Math.random() * 100)

Properties of the Math.object()

In addition, the Math object contains eight static properties (constants). They are rarely used in development but, nevertheless, a good developer should know what they are.

// Euler's number
Math.E        // 2.718281828459045

// PI
Math.PI       // 3.141592653589793

// the square root of 2
Math.SQRT2    // 1.4142135623730951

// the square root of 1/2
Math.SQRT1_2  // 0.7071067811865476

// the natural logarithm of 2
Math.LN2      // 0.6931471805599453

// the natural logarithm of 10
Math.LN10     // 2.302585092994046

// base 2 logarithm of E
Math.LOG2E    // 1.4426950408889634

// base 10 logarithm of E
Math.LOG10E   // 0.4342944819032518

Conclusion

The built-in Math object is one of the important tools in JavaScript. With it, we can round a number with methods like Math.round(), Math.ceil(), Math.floor() and Math.trunc(), raise it to a power and take an absolute value with methods like Math.pow() and Math.abs(), take a square root with Math.sqrt(). Also, we can check if a number is positive or negative with Math.sign(), get the smallest and the largest number with Math.min() and Math.max(), as well as find a random number with Math.random().

27 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo