Javascript Math Function Or Properties

In the below guide, we give you ideas about javascript important math function codes that looks short but in respect to use, it has important role in JS. The codes are abs, rand, floor, ceil, log, etc.

JavaScript have eight mathematical constants for math solutions and that are below:

Math.PI       //PI - 3.141592653589793
Math.E        //Euler's number - 2.718281828459045
Math.SQRT2    //the square root of 2 - 1.4142135623730951
Math.SQRT1_2  //the square root of 1/2 - 0.7071067811865476
Math.LOG2E    //base 2 logarithm of E - 1.4426950408889634
Math.LN10     //the natural logarithm of 10 - 2.302585092994046
Math.LOG10E   //base 10 logarithm of E - 0.4342944819032518
Math.LN2      //the natural logarithm of 2 - 0.6931471805599453

Now what is math methods and how we do use these. To run the math methods we use the below syntax:

Math.method.(number)

where method is a name of method that you want to use.

Number to integer convert we use four methods that are round, ceil, floor, and trunc.

Round Method

The round method convert the float number into round number.

Example:


Math.round(8.9); // returns 9
Math.round(8.7); // returns 9
Math.round(8.4); // returns 8
Math.round(8.2);  // returns 8
Math.round(-8.2);  // returns -8

Ceil Method

It returns the rounded up value to its nearest integer.

Example:

Math.ceil(5.9);   // returns 6
Math.ceil(5.7);   // returns 6
Math.ceil(5.4);   // returns 6
Math.ceil(5.2);   // returns 6
Math.ceil(-5.2);  // returns -5

Floor Method

It returns the rounded down value to its nearest integer:

Math.floor(5.9);    // returns 5
Math.floor(5.7);    // returns 5
Math.floor(5.4);    // returns 5
Math.floor(5.2);    // returns 5
Math.floor(-5.2);    // returns -6

Trunc Method

It returns only the integer part of value.

Example:

Math.trunc(6.9);  // returns 6
Math.trunc(6.7);  // returns 6
Math.trunc(6.4);  // returns 6
Math.trunc(6.2);  // returns 6
Math.trunc(-6.2); // returns -6

Pow Method

pow method set the second value as a power of first value.

Example:

Math.pow(9, 2); // returns 81

Here 81 come with the 9 multiplcation in 2 times like 9 X 9 = 81

Sqrt Method

It give square root of shared inter value.

Example:

Math.sqrt(81);      // returns 9

Abs Method

It give positive value like below example.

Example:


Math.abs(-5.7);     // returns 5.7

Hope these function will help you to get more understanding about math methods and properties. For other tutorials, you can visit our javascript function list.

Stay in touch. Ask Questions.
Lean on us for help, strategies and expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *