Web Development

JavaScript Math

JavaScript Math is section 9 of Beginner’s Guide to JavaScript.

We already know about mathematical operations of adding, subtracting, multiplying and division. How about doing more? Like finding the power of value or log value. Well you cannot do it without the Math object. The Math object allows you to perform mathematical tasks on numbers. It includes several mathematical methods and you can use them to perform number of mathematical operations using it. Math has no constructor. No methods have to create a Math object first.

Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between
-PI/2 and PI/2 radians
atan2(y,x) Returns the arctangent of the quotient of its arguments
ceil(x) Returns x, rounded upwards to the
nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the
nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x,y,z,…,n) Returns the number with the highest value
min(x,y,z,…,n) Returns the number with the lowest value
pow(x,y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle

Math.random()

Returns a random number betwween 0 and 1.

Math.random();              // returns a random number

Math.min() and Math.max()

Used to find the lowest or highest value in a list of arguments.

Math.min(0, 150, 40, 80, -8);      // returns -8

Math.max(0, 150, 40, 80, -8);      // returns 150

Rounding Numbers

There are few ways of rounding a number. Rounding up, rounding down and rounding to the nearest integer.

  • Math.round()

    • Rounds a number to the nearest integer.Math.round(4.7);            // returns 5
      Math.round(4.4);            // returns 4
  • Math.ceil()

    • Rounds a number up to the nearest integer.Math.ceil(4.4);             // returns 5
  • Math.floor()

    • Rounds a number down to the nearest integer.Math.floor(4.7);            // returns 4

Math.floor() and Math.random() can be used together to return a random number between 0 and 10.

Math.floor(Math.random() * 11);   // returns a random number between 0 and 10

Math Constants

JavaScript provides 8 mathematical constants that can be accessed with the Math object.

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


 

What’s Next?

Learn what includes in JavaScript Array

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW