Math Object In JavaScript

Introduction

 
In the previous chapter, we learned about objects, and its types in JavaScript with example program.
 
In this chapter, we will learn about Math Object in JavaScript and how it is used in programs of JavaScript.
 

Math Object

 
In programming there are normally situations where we want to do mathematical calculations;  for this purpose programming languages provides some built-in functions. Like other programming languages, JavaScript also provides a “Math” object to perform mathematical operations. Math object has properties and methods that help to perform mathematical tasks. Math object is not a constructor. All methods and properties of Math are static, so they can be accessed by using Math as object without creating one.
 

Math Properties

 
Math object has the following properties.
 
Property Description
E Returns Euler’s constant, approximate value 2.718
LN2 Returns the natural logarithm of 2, approximate value 0.693
LN10 Returns the natural logarithm of 10, approximate value 2.302
LOG2E Returns base 2 logarithms of E, approximate value 1.442
LOG10E Returns base 10 logarithms of E, approximate value 0.434
PI Returns PI, approximate value 3.14
SQRT1_2 Returns square root of ½, approximate value 0.707
SQRT2 Returns square root of 2, approximate value 1.414
 
Above listed any property can be accessed as follows.
 
var pi=Math.PI;
 

Math Methods

 
Math object provides the following methods for mathematical operations.
  1. abs()
     
    The abs () method returns the absolute value of the number. This method takes one argument number.
     
    Syntax- Math.abs(x)
     
    Example
    1. var result=Math.abs(-10);  
    2. O/P: result=10  
  2. acos()
     
    The acos () method returns the arccosine of number in radians. It takes one argument. This method returns numeric values between 0 to PI radians for argument between -1 to 1, otherwise, it returns NaN.
     
    Syntax- Math.acos(x)
     
    Example
    1. var result=Math.acos(-1);  
    2. O/P: result= 3.141592653589793  
  3. asin()
     
    The asin() method returns the arcsine of number in radians. It takes one argument. This method returns numeric values between -PI/2 and PI/2 radians for argument between -1 to 1, otherwise it returns NaN.
     
    Syntax- Math.asin(x)
     
    Example
    1. var result=Math.asin(-1);  
    2. O/P: result= 1.5707963267948966  
  4. atan()
     
    The atan() method returns the arctangent of a number in radians. It takes one number as argument. This method returns values between –PI/2 and PI/2 radians.
     
    Syntax- Math.atan(x)
     
    Example
    1. var result=Math.atan(20);  
    2. O/P: result= 1.5208379310729538  
  5. cos()
     
    The cos () method returns the cosine of a number. It takes one argument and returns its cosine.
     
    Syntax- Math.cos(x)
     
    Example
    1. var result=Math.cos(20);  
    2. O/P: result= 0.40808206181339196  
  6. sin()
     
    The sin () method returns numeric values between -1 to 1, which represents the sin of the angle given.
     
    Syntax- Math.cos(x)
     
    Example
    1. var result=Math.sin(20);  
    2. O/P: result= 0.9129452507276277  
  7. tan()
     
    The tan () method returns a numeric value that represents tangent of a given angle.
     
    Syntax- Math.tan(x)
     
    Example
    1. var result=Math.tan(90);  
    2. O/P: result= -1.995200412208242  
  8. ceil()
     
    The ceil () method returns the smallest integer greater than or equal to the given number.
     
    Syntax- Math.ceil(x)
     
    Example
    1. var result=Math.ciel(10.2);  
    2. O/P: result= 11  
  9. floor()
     
    The floor () method returns the smallest integer less than or equal to the given number.
     
    Syntax- Math.floor(x)
     
    Example
    1. var result=Math.floor(10.2);  
    2. O/P: result= 10  
  10. max()
     
    The max () method returns the largest number from given argument values. If no arguments are given then it returns infinity. If at least one of the arguments cannot be converted into a number then the result is NaN.
     
    Syntax- Math.max(x1, x2, x3, …)
     
    Example
    1. var result=Math.max(10.2,11,12);  
    2. O/P: result= 12  
  11. min()
     
    The min () method returns the smallest number from given argument values. If no arguments are given then it returns infinity. If at least one of the arguments cannot be converted into a number then the result is NaN.
     
    Syntax- Math.min(x1, x2, x3, …)
     
    Example
    1. var result=Math.min(10.2,10,11);  
    2. O/P: result= 10  
  12. exp()
     
    The exp () method returns Ex, where x is argument and E is Euler’s constant, the base of the natural logarithms.
     
    Syntax- Math.exp(x)
     
    Example
    1. var result=Math.exp(1);  
    2. O/P: result= 2.718281828459045  
  13. log()
     
    The log () method returns the natural logarithm (base E) of a given number. If the number is negative then it returns NaN.
     
    Syntax- Math.log(x)
     
    Example
    1. var result=Math.log(1);  
    2. O/P: result= 0  
  14. pow()
     
    The pow () method takes two arguments and returns the base to the exponent power. Means if x and y are argument it returns value xy.
     
    Syntax- Math.pow(x,y)
     
    Example
    1. var result=Math.pow(2,3);  
    2. O/P: result= 8  
  15. random ()
     
    The random () method returns pseudo-random number in between 0 and 1.
     
    Syntax- Math.random()
     
    Example
    1. var result=Math.random();  
    2. O/P: result= 0.542335860034119  
  16. round ()
     
    The round () method returns the value of a number rounded to the nearest integer.
     
    Syntax- Math.round(x)
     
    Example
    1. var result=Math.round(2.3);  
    2. O/P: result= 2  
  17. sqrt ()
     
    The sqrt () method returns the square root of the number.
     
    Syntax- Math.sqrt(x)
     
    Example
    1. var result=Math.sqrt(4);  
    2. O/P: result= 2  
Note
 
A lot of the math methods have a precision that’s implementation-dependent. This means that different browsers can give different results, and even the same JS engine on different OS or architecture can give different results.
 

Summary

 
In this chapter, we learned about Math Object and its uses in JavaScript.
Author
Ranjit Powar
294 6.3k 1.5m
Next » Array in JavaScript