Javascript Math tutorial

In this tutorial, we will learn about Math, a built-in object that provides mathematical constants and functions in JavaScript.

Math object #

The Math object is a built-in object, but it is not a constructor. That is, the Math object does not create instances, and all constants and functions are provided as static properties and methods of the Math object. For example, the Math.PI property is a constant that represents pi, and the Math.sin method is a function that calculates the sine from a radian value. In the following example, we are calculating the sine at 90 degrees, and since the sine of 90 degrees is 1, the sin90 variable returns 1.

const rad90 = Math.PI * 90 / 180;
const sin90 = Math.sin(rad90);
console.log(sin90); // => 1

Many functions and constants, including trigonometric functions, are provided by Math objects. In this chapter, we will introduce some of the most commonly used ones with use cases. For an exhaustive explanation, please refer to the MDN reference.

Generating random numbers #

One of the main uses of the Math object is to generate random numbers using the Math.random method. The Math.random method returns a pseudo-random floating point number in the range between 0 and 1.

for (let i = 0; i < 5; i++) {
    // return a random floating point number each time
    console.log(Math.random());
}

In the following example, we use the Math.random method to generate random numbers in an arbitrary range.

// minからmaxまでの乱数を返す関数
function getRandom(min, max) {
    return Math.random() * (max - min) + min;
}
// 1以上5未満の浮動小数点数を返す
console.log(getRandom(1, 5));

Comparing the size of numbers #

The Math.max method returns the largest of the multiple numbers passed as arguments. Similarly, the Math.min method returns the minimum of the numbers passed as arguments.

console.log(Math.max(1, 10)); // => 10
console.log(Math.min(1, 10)); // => 1

These methods take variable length arguments, so any number of numbers can be compared. When extracting the largest or smallest value from an array of numbers, use the ... (spread syntax) for a concise description.

const numbers = [1, 2, 3, 4, 5];
console.log(Math.max(. .numbers)); // => 5
console.log(Math.min(. .numbers)); // => 1

Rounding a number to an integer #

The Math object has several methods for rounding numbers to integers. The most common are the Math.floor method, which truncates the decimal point, the Math.ceil method, which raises the decimal point, and the Math.round method, which rounds.

The Math.floor method returns the largest integer that is less than or equal to the number passed as an argument. Such a function is called a floor function. A positive number, 1.3, will be rounded to 1, while a negative number, -1.3, will be rounded to a smaller integer, -2.

The next Math.ceil method returns the smallest integer that is greater than or equal to the number passed as an argument. Such a function is called a ceiling function. A positive number, 1.3, will be rounded to 2, while a negative number, -1.3, will be rounded to the larger integer -1.

The Math.round method performs the general rounding process. If the decimal part is less than 0.5, it is rounded down, otherwise it is rounded up.

// Bottom function
console.log(Math.floor(1.3)); // => 1
console.log(Math.floor(-1.3)); // => -2
// Ceiling function
console.log(Math.ceil(1.3)); // => 2
console.log(Math.ceil(-1.3)); // => -1
// rounding
console.log(Math.round(1.3)); // => 1
console.log(Math.round(1.6)); // => 2
console.log(Math.round(-1.3)); // => -1

Summary #

In this chapter, we learned about the Math object. Since the methods introduced are only a part of the Math object, there are other methods available.

  • Math is a built-in object that provides mathematical constants and functions
  • Math is not a constructor and cannot be instantiated
  • Methods are provided to generate pseudo-random numbers, compare numbers, calculate numbers, etc.

Tags:

Javascript