Math Objects in JavaScript

Introduction

So many operations in mathematics are required for many functions in web platforms. Those operations are in the Math object of JavaScript objects.

Math Object in JavaScript

The Math object is used to perform simple and complex arithmetic operations. 

The main properties and methods of Math Objects are given below.

Properties of Math Objects in JavaScript

  • E- Holds the Euler's Number 
  • LN2 - Natural Logarithm of 2.
  • LN10 - Natural Logarithm of 10.
  • LOG2E - Base 2 Logarithm of E.
  • LOG10E - Base 10 Logarithm of E.
  • PI - Numerical Value of PI(22/7 or 3.142)
  • SQURT1_2 - Square root of 2.
  • SQRT2- Square root of 2.

These are some main properties of Math objects in JavaScript. There is a long list of methods for the Math objects; some are given below. Now let's learn about the methods in JavaScript. 

Methods of Math Objects in JavaScript

  • abs (x) - Return the absolute value of x.
  • acos (x) - Return the arccosine (in radian) of x.
  • asin (x) - Return arcsine (in Radian) of x.
  • atan (x) - Returns the arctangent of x with numeric value b/w -PI/2 to +PI/2.
  • atan2 (y,x) - Returns the arctangent of quotient on dividing y and x.
  • ceil (x) - Rounds up x to the nearest bigger integer.
  • cos (x) - Return cosine value of x.
  • exp (x) - Returns the value of ex.
  • floor (x) - Rounds up x to the nearest smaller integer. 
  • log (x) - Returns the natural logarithmic value of x.
  • max (x,y,z,....n) - Returns the highest number from the given list.
  • min (x,y,z,.......) - Returns the smallest number from the given list. 
  • pow (x,y)- Returns the x to the power of y.
  • random ()- Returns a Random number.
  • round (x)- Rounds up x to the nearest integer.
  • sin (x) - Return the sine value of x.
  • sqrt(x) - Returns the square root of x.
  • tan (x) - Returns the tangent value of x.

That was a little bit about the Math Objects of JavaScript. Now let's do some exercises. 

In the following exercise, we are trying to print the value of a given method.

Watch

<!DOCTYPE HTML>
<HEAD>
<TITLE>Math Functions</TITLE>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
document.write("Floor: "+Math.floor(12.6)+"<BR/>"); document.write("Log: "+Math.log(12.6)+"<BR/>");
document.write("Max: "+Math.max(12,34,15,65)+"<BR/>");
document.write("Min: "+Math.min(12,34,15,65)+"<BR/>"); document.write("Pow: "+Math.pow(10,2)+"<BR/>"); document.write("Random: "+Math.random()+"<BR/>"); document.write("Round: "+Math.round(12.6)+"<BR/>"); document.write("Sin: "+Math.sin(45)+"<BR/>");
document.write("Sqrt: "+Math.sqrt(16)+"<BR/>");
document.write("Tan: "+Math.tan(45)+"<BR/>");
</SCRIPT>
</BODY>
</HTML>

Various mathematical operations are performed in the preceding code using a math object.

  • The Math.floor() method rounds up the number to the smallest number.
  • The Math.log() method finds the logarithmic value of the given number.
  • The Math.max() method finds the maximum number from the given list.
  • The Math.min() method finds the minimum number from the given list.
  • The Math.pow(10,2) method finds the value of 10 to the power of 2.
  • The Math.random() method returns a random number between 0 and 1.
  • The Math.round() method rounds up the number to the nearest more significant number.
  • The Math.sin() method finds the sin value.
  • The Math.sqrt() method finds out the tangent of a given number.
  • The Math.tan() method finds the Tan value.

So the output for the code above will be,

Floor: 12
Log: 2533606813957432
Max: 65
M12
Pow: 100
Random: 0.25858352379673395
Round: 13
S 0.8500035245341184 Sight:4
Tan: 16197751905435615


Similar Articles
Foreantech
Foreantech - A complete online solution company.