JavaScript Math Object

JavaScript Math Object

Math Object

1. pi value property

  • it gives constant famous value as per name
console.log(Math.PI); 
//OUTPUT = 3.14159265358...

2. Math. pow()

Math.pow(x, y) returns the value of x to the power of y

    console.log(Math.pow(2, 3)); 
    //2*2*2 
    //8
    console.log(2**4); 
    //2*2*2*2//16

3. Math. sqrt()

  • Math. sqrt(x) returns the square root of x
 console. log(Math.sqrt(25)); //5
 console. log(Math.sqrt(81)); //9
 console. log(Math.sqrt(66)); //8.12403840463596

4. Math.abs()

  • Math.abs(x) returns the absolute (positive) value of x
console.log(Math.abs(-55)); //55
 console. log(Math.abs(-55.5)); //55.5
 console.log(Math.abs(-955)); //955
 console.log(Math.abs(4-6)); //2

5.Math.round() property

  • returns the value of x rounded to its nearest integer
let num = 10.2565;
console.log(Math.round(num));

6.Math.ceil()

Math.ceil(x) returns the value of x rounded up to its nearest integer

 console.log(Math.ceil(4.4)); //5
 console.log(Math.ceil(99.1)); //100

7. Math.floor()

Math.floor(x) returns the value of x rounded down its nearest intger

console.log(Math.floor(4.9)); //4
console.log(Math.floor(0.6)); //0

8.Math.min()

  • its use for the find minimum value from the given argument
console.log(Math.min(200,-3,-9,25,0,46)); //-9

9.Math.max()

its use for the find maximum value from the given argument

console.log(Math.max(200,-3,-9,25,0,46)); //200

10.Math.random()

it returns a random number between 0(inclusive), and 1(exclusive)

console.log(Math.random()); //0.xxxxxxx
console.log(Math.random() * 10); //0.xxxxxxxxx * 10 = x.xx
  • to round of(10 exclusive thatsy use floor otherwise round of can use)
console.log(Math.floor(Math.random() * 10)); //0.xxxxxxxxx * 10 = x.xx = x

11.Math.trunc()

  • its returns the integer part of a number

       console.log(Math.trunc(4.6)); //4
       console.log(Math.trunc(-99.1)); //-99