JavaScript Fundamentals / Objects

Math Object

➗ Exploring Important Math Properties

The JavaScript Math object allows you to perform mathematical tasks. The Math object is static. All methods and properties can be used without creating a Math object first.

Math.PI;

📐 Math Properties (Constants)

The syntax for any Math property is : Math.property.

JavaScript provides 8 mathematical constants that can be accessed as Math properties:

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E

🔢 Math Methods

The syntax for Math any methods is : Math.method(number)


1️⃣ Math.round( )

Math.round(x) returns the nearest integer:

Examples :

Math.round(4.6);  //5
Math.round(4.5);  //5
Math.round(4.4);  //4

2️⃣ Math.ceil( )

Math.ceil(x) returns the value of x rounded up to its nearest integer:

Example :

Math.ceil(4.9);  //5
Math.ceil(4.7);  //5
Math.ceil(4.4);  //5
Math.ceil(4.2);  //5
Math.ceil(-4.2); //-4

3️⃣ Math.floor( )

Math.floor(x) returns the value of x rounded down to its nearest integer:

Example :

Math.floor(4.9);  //4
Math.floor(4.7);  //4
Math.floor(4.4);  //4
Math.floor(4.2);  //4
Math.floor(-4.2); //-5

4️⃣ Math.trunc( )

Math.trunc(x) returns the integer part of x:

Example :

Math.trunc(4.9);   //4
Math.trunc(4.7);   //4
Math.trunc(4.4);   //4
Math.trunc(4.2);   //4
Math.trunc(-4.2);  //-4

5️⃣ Math.sign( )

Math.sign(x) returns if x is negative, null or positive.

  • If x is positive it returns 1

  • If x is negative it returns -1

  • If x is zero, it returns 0

Example :

Math.sign(-4);  //-1
Math.sign(0);   //0
Math.sign(4);   //1

6️⃣ Math.pow( )

Math.pow(x, y) returns the value of x to the power of y:

Example :

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

7️⃣ Math.sqrt( )

Math.sqrt(x) returns the square root of x:

Example :

Math.sqrt(64); //8

8️⃣ Math.abs( )

Math.abs(x) returns the absolute (positive) value of x:

Example :

Math.abs(-4.7); //4.7

9️⃣ Math.sin( )

  • Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example :

Math.sin(90 * Math.PI / 180);     // returns 1 (the sine of 90 degrees)

🔟 Math.cos( )

  • Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).

If you want to use degrees instead of radians, you have to convert degrees to radians:

Angle in radians = Angle in degrees x PI / 180.

Example :

Math.cos(0 * Math.PI / 180);     // returns 1 (the cos of 0 degrees)

1️⃣1️⃣ Math.min( ) and Math.max( )

Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:

Example 1 :

Math.min(0, 150, 30, 20, -8, -200);  //-200

Example 2 :

Math.max(0, 150, 30, 20, -8, -200);  //150

1️⃣2️⃣ Math.random( )

Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):

Example :

Math.random();  //Random number between 0 and 1 (e.g., 0.7245)

✅ Note: Output changes every time; always between 0 (inclusive) and 1 (exclusive).


1️⃣3️⃣ The Math.log( ) Method

  • Math.log(x) returns the natural logarithm of x.

  • The natural logarithm returns the time needed to reach a certain level of growth:

  • Math.E and Math.log() are twins.

Examples :

Math.log(1);  //0
Math.log(2);  //~0.6931
Math.log(3);  //~1.0986

1️⃣4️⃣ The Math.log2( ) Method

Math.log2(x) returns the base 2 logarithm of x.

Example : How many times must we multiply 2 to get 8?

Math.log2(8);  //3

1️⃣5️⃣ The Math.log10( ) Method

Math.log10(x) returns the base 10 logarithm of x.

Example : How many times must we multiply 10 to get 1000?

Math.log10(1000); //3

🔰Summary Table

Category

Methods Included

🎯 Rounding method

round(), ceil(), floor(), trunc()

➕Power/Root

pow(), sqrt(), abs()

📐 Trigonometry

sin(), cos()

➗Logarithmic

log(), log2(), log10()

🎲Random/Min/Max

random(), min(), max()

🔮Miscellaneous

sign()

©️Constants

PI, E, SQRT2, etc.