JavaScript Introduction | Standard Objects | Math Object

Math Object

The Math object is a JavaScript standard built-in object that pre-implements constants and functions frequently used in mathematics.

Unlike other global objects, the Math object does not have a constructor. Therefore, you can use all methods and properties of the Math object directly without creating a separate instance.

Math Methods

JavaScript provides various Math methods so mathematical work can be performed easily on web pages. The most commonly used representative Math methods are as follows.

  1. Math.min()
  2. Math.max()
  3. Math.random()
  4. Math.round()
  5. Math.floor()
  6. Math.ceil()
  7. Math.sin()

Most Math methods are likely to produce different results depending on the web browser. Even the same JavaScript interpreter can return different results if the operating system is different. Therefore, it is better not to use Math methods for work that requires very precise results.

Math.min() Method

The Math.min() method returns the smallest number among the values passed as arguments. If no argument is passed, it returns Infinity, and if any argument contains a value that cannot be compared, it returns NaN.

Math.min();                              // Infinity
Math.min(1, 10, -100, -10, 1000, 0);     // -100
Math.min(1, 10, -100, -10, "-1000", 0);  // -1000
Math.min(1, 10, -100, -10, "string", 0); // NaN

Math.max() Method

The Math.max() method returns the largest number among the values passed as arguments. If no argument is passed, it returns -Infinity, and if any argument contains a value that cannot be compared, it returns NaN.

Math.max();                              // -Infinity
Math.max(1, 10, -100, -10, 100, 0);      // 100
Math.max(1, 10, -100, -10, "1000", 0);   // 1000
Math.max(1, 10, -100, -10, "string", 0); // NaN

Math.random() Method

The Math.random() method returns a random number greater than or equal to 0 and less than 1.

var min = 10, max = 20;
Math.random();                     // [0, 1)
Math.random() * (max - min) + min; // [min, max)

In the example above, the ‘[’ symbol means “greater than or equal to,” and the ‘]’ symbol means “less than or equal to.”
Also, the ‘(’ symbol means “greater than,” and the ‘)’ symbol means “less than.”

Math.round() Method

The Math.round() method rounds the value passed as an argument at the first decimal place and returns the result.

Math.round(10.49);  // 10
Math.round(10.5);   // 11
Math.round(-10.5);  // -10
Math.round(-10.51); // -11

Math.floor() Method

The Math.floor() method returns the largest integer among numbers less than or equal to the value passed as an argument.

Math.floor(10.95);  // 10
Math.floor(11.01);  // 11
Math.floor(-10.95); // -11
Math.floor(-11.01); // -12

Math.ceil() Method

The Math.ceil() method returns the smallest integer among numbers greater than or equal to the value passed as an argument.

Math.ceil(10.95);  // 11
Math.ceil(11.01);  // 12
Math.ceil(11);     // 11
Math.ceil(-10.95); // -10
Math.ceil(-11.01); // -11

Math.sin() Method

The Math.sin() method returns the sine function value of the value passed as an argument.

Math.sin(0);           // 0
Math.sin(Math.PI / 2); // 1

All JavaScript trigonometric methods use radians as the unit of angle. To convert between radians and degrees, use the following formula.

Syntax

radianValue = degreeValue * (Math.PI / 180)

Math.PI is a JavaScript constant that represents the pi (π) value used in mathematics.
Its approximate value is 3.14159.