Math


What is math?

Javascript provides various features which make coding more straightforward, and various operations are done with a single line of code. Math is also one of those features which provide various math activities in which you have to add numbers, and the operation takes place automatically. Math is a predefined library function that allows us to perform all mathematical operations. As other properties like Number Boolean have constructor, Math property has no constructor. Math is a static property that contains various methods to perform respective operations on given data.

Description

The syntax of the math property with the method is, ‘math.property’. There are various methods; some are as follows.

  1. Math.E // returns Euler’s number
  2. Math.PI // returns PI
  3. Math.SQRT2 // returns square root of 2
  4. Math.SQRT1_2 // returns square root of 1/2
  5. Math.LN2 // returns natural logarithm of 2
  6. Math.LN10 // returns natural logarithm of 10
  7. Math.LOG2E // returns base 2 logarithm of E
  8. Math.LOG210 // returns base 10 logarithm of E

There are some common methods which perform operations on numbers.
Math.method(Number)

  1. Math.round(x) // Rounded to its nearest integer
  2. Math.ceil(x) // Rounded to its nearest integer
  3. Math.floor(x) // Rounded down to its nearest integer
  4. Math.trunc(x) // Return the integer part of the number.

Significance of Math function

Math function is a handy function that helps to reduce the code, as it performs various operations in a single line and makes coding easier. If you want to implement this property in your code, then you have to learn javascript from different online platforms.

Example No.1:

<html> <body> <h4>Knowledge2life</h4> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = Math.PI; </script> </body> </html>

OUTPUT:

Knowledge2life

3.141592653589793

Example No.2:

<html> <body> <h4>Knowledge2life</h4> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "<p><b>Math.E:</b> " + Math.E + "</p>" + "<p><b>Math.PI:</b> " + Math.PI + "</p>" + "<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" + "<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" + "<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" + "<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" + "<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" + "<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>"; </script> </body> </html>

OUTPUT:

Knowledge2life

Math.E:2.718281828459045
Math.PI:3.141592653589793
Math.SQRT2:1.4142135623730951
Math.SQRT1_2:0.7071167822811865476
Math.LN2:0.6931471805599453
Math.LN10:2.302585092994046
Math.LOG2E:1.4426950408889634
Math.Log10E:0.4342944819032518