Exploring the Math Class in JavaScript

Math is a built-in object in JavaScript that comes with various mathematical constants and methods, which allows you to perform more complex mathematical tasks that the basic arithmetic operators cannot. For example,

javascript
1console.log(Math.E); // returns Euler's number
2console.log(Math.PI); // returns PI
3console.log(Math.SQRT2); // returns the square root of 2
4console.log(Math.SQRT1_2); // returns the square root of 1/2
5console.log(Math.LN2); // returns the natural logarithm of 2
6console.log(Math.LN10); // returns the natural logarithm of 10
7console.log(Math.LOG2E); // returns base 2 logarithm of E
8console.log(Math.LOG10E); // returns base 10 logarithm of E
text
12.718281828459045
23.141592653589793
31.4142135623730951
40.7071067811865476
50.6931471805599453
62.302585092994046
71.4426950408889634
80.4342944819032518

Besides the constants, there are also a few built-in Math methods. For instance, the abs() method returns the absolute value of a given number.

javascript
1console.log(Math.abs(10));
2console.log(Math.abs(-10));
text
110
210
Notice that unlike Date(), Math is an object, not a constructor.

Most of these methods are only useful when performing math-related tasks, and based on their names, you can probably guess what they do.

For this lesson, we are only going to cover some of the methods that are more common for daily uses.

Rounding numbers

Wait, there is more!

You need an account to access the rest of this lesson. Our course is 50% off for limited time only! Don't miss the opportunity!

🎉 Create an Account 🎉