JavaScript Math Object is used to get Maths Properties ( to get constants) and methods like pi, sinθ, random number, Square root, power etc.

JS Math Object

JS Math Object is a build in JavaScript Object to get Mathematical properties, Mathematical constants and Math based methods.


    var x=Math;            // return math object    
    var pi=Math.PI;        // return value of pie
        

Math Object List



Mathematical Constants

JavaScript supports Eight Properties to get Maths Constants

JavaScript Maths Constants
Property Value Meaning
Math.PI3.141592653589793value of PI
Math.SQRT21.4142135623730951square root of 2
Math.SQRT1_20.7071067811865476reciprocal of square root of 2,
i.e, 1 divided by square root of 2
Math.E2.718281828459045Euler's Constant
Math.LN20.6931471805599453Natural logarithm of 2
Math.LN102.302585092994046Natural logarithm of 10
Math.LOG2E1.4426950408889634Log base 2 of Euler's Constant
Math.LOG10E0.4342944819032518Log base 10 of Euler's Constant

Maths Operations

Maths Object have several methods to perform Maths Operations. For exp


Math.abs()

Math.abs() method returns the positive value of given number. For positive number, it will remain same, but for negative numbers, it will convert it to positive.


    Math.abs(2);                // return 2
    Math.abs(-2);               // return 2
    Math.abs(0);                // return 0

Math.ceil()

Math.ceil() method will round a number up to next integer.


    Math.ceil(2.3);                // return 3
    Math.ceil(1.9);                 // return 2
    Math.ceil(2);                   // return 2

Math.floor()

Math.ceil() method will round a number down to next integer.


    Math.floor(2.3);                // return 2
    Math.floor(1.9);                 // return 1
    Math.floor(2);                   // return 2

Math.round()

Math.round() method will round a number to nearest integer.


    Math.round(2.3);                 // return 2
    Math.round(1.9);                 // return 2
    Math.round(2);                   // return 2

Math.exp()

Math.exp() method will raise a number to the power of Eular's Constant


    Math.exp(1);                 // return 2.718281828459045
    Math.exp(0);                 // return 1

Math.pow()

Math.pow() method will raise any no (1st argument ) to power of (2nd argument ).


    Math.pow(2,3);                 // return 8
    Math.pow(3,4);                 // return 81

Math.sqrt()

Math.sqrt() method will return the square root of a number.


    Math.sqrt(4);                   // return 2
    Math.sqrt(81);                  // return 9

Math.log()

Math.log() method will return the natural logarithm of a number.


    Math.log(1);                   // return 0
    Math.log(2);                   // return 0.6931471805599453
    Math.log(Math.E);              // return 1

Math.max()

Math.max() method will return the maximum number from given arguments.


    Math.max(1,2);                   // return 2
    Math.max(3,5);                   // return 5

Math.min()

Math.log() method will return the minimum number from given arguments.


    Math.min(1,2);                   // return 1
    Math.min(2,3);                   // return 2

Trigonometric Functions

Trigonometry functions are also used in javascript to work with geometrical objects. JavaScript supports all trigonometry like, sin, cos, tan etc.

Math.sin()

Math.sin() method is used return sine of angle.

The default unit is radians, i.e PI.

To get sine of deg, use (radian * Math.PI/180).


    Math.sin(90);                // 0.8939966636005579
             
    Math.sin(90*Math.PI/180);    // 1         
    Math.sin(30*Math.PI/180);    // 0.49999999999999994         

Math.cos()

Math.cos() method is used return cosine of angle.


    Math.cos(0);                // 1
    Math.cos(30);               // 0.15425144988758405
       

Math.tan()

Math.tan() method is used return tangent of angle.


    Math.tan(0);                 // 0
    Math.tan(30);                // -6.405331196646276
    Math.tan(45);                // 1.6197751905438615
    Math.tan(60);                // 0.320040389379563
       

To get cosec, sec and cot, divide sin, sec or tan by 1. exp

  1. cosec 30 = 1/(Math.sin(30*Math.PI/180))
  2. sec 45 = 1/(Math.cos(45*Math.PI/180))
  3. cot 60 = 1/(Math.tan(60*Math.PI/180))

Random Numbers

Math.random() method is used to get any random number between 0 and 1.


    Math.randon();        // any random no between 0 to 1  
        

To get random number between 0 and any other number, multiply Math.random() with the same number.


    Math.randon()*10;        // any random no between 0 to 10
    Math.randon()*100;        // any random no between 0 to 100
         
        

Get random integer

To get random integer, use Math.floor(Math.random).


    Math.floor(Math.random()*10);         
        

Build a DICE

To get random integer between 1-6, use Math.floor(Math.random()*6+1)


    Math.floor(Math.random()*6+1);