The Math Object in JavaScript:

The math object allows you to do mathematical operations in JavaScript. You can perform math functionality on numbers.

Example:

In following example we creating random numbers between 0 and 1 by using math.random() function

Here is some common and frequently used math function with description and separate examples.

Math.max() and Math.min() :

The Math.max() function is use to find the highest value from given arguments and Math.min() function is use to find lowest value from given arguments.

Example:

Math.random() :

The Math.random() function is use to find random numbers between 0 and 1.

Example: We already have seen the example of random above.

Math.round():

The Math.round() function returns the nearest integer of that number.

Example:

Math.ceil() and Math.floor():

The Math.ceil() function returns up to the nearest integer of a number and the Math.ceil() function returns down to the nearest integer of a number.

Example:

Here is a one example in which we are using Math.ceil() and Math.floor()

Math Constants:

There are 8 mathematical constants that can be accessed by math object in JavaScript.

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

Example:

 

One response to “The Math Object in JavaScript”

  1. Elmer Lillis says:

    very interesting points you have noted, thanks for putting up.

Leave a Reply

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