The Math Object of JavaScript

There are some math objects of JavaScript are given below:

  • Math.PI: If we want use the value of PI (3.14159) in mathematical operation then we can use simply Math.PI.
var area=(Math.PI)*radious*radious;
  • abs() : The abs() method returns the absolute value of the number passed as its parameter. Essentially, this means that it returns the positive value of the number. So, -1 is returned as 1.
var num=-101;
num=Math.abs(num);
  • cell() : The cell() method always rounds a number up to the next largest whole number or integer. Ao, 10.01 becomes 11, and -9.99 becomes -9. Using ceil() is different from using the parseInt() function, because parseInt() simply chops off any numbers after the decimal point to leave a whole number, whereas cell() rounds the number up. parseInt() is a native JavaScript function and not a method of the Math object.
var num=101.01;
num=Math.ceil(num);
num= parseInt(num);
  • floor() : The floor() method removes any numbers after the decimal point, and returns a whole or integer.
  • round() : The round() method rounds up only if the decimal part is .5 or greater, and rounds down otherwise.
  • random() : The random() method returns a random floating-point number in the range between 0 and 1, where 0 is included and 1 is not. This can be very useful for displaying random banner images or for writing a JavaScript game. If we wanted a random number between 1 and 100, we would just change the code so the Math.random() is multiplied by 100.
  • pow() : The pow() method raises a number to a specified power. Math.pow(2,8)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s