Wednesday, September 12, 2018

JavaScript - Built in Functions - Math Object

In JavaScript, we may use some built in objects. One amongst them is the Math object.
Some useful examples using Math object:

var myPI = Math.PI;
console.log(myPI);

var myE = Math.E;
console.log(myE)

var a = -5.5648;
console.log(Math.abs(a))
console.log(Math.ceil(a))
console.log(Math.floor(a))
console.log(Math.exp(10)); // e to the power of 10
console.log(Math.log(10)); // log of 10 to the base e
console.log(Math.max(10,20,30,40,50,60,70,80,90,100)); 
console.log(Math.min(10,20,30,40,50,60,70,80,90,100)); 
console.log(Math.random()); // Generates a random number between 0 and 1
console.log(Math.random()*100); // To get a random number between 0 and 100
console.log(Math.floor(Math.random()*100 + 1)); // To get a random integer between 1 and 100




Console Output:

  • 3.141592653589793
  • 2.718281828459045
  • 5.5648
  • -5
  • -6
  • 22026.465794806718
  • 2.302585092994046
  • 100
  • 10
  • 0.1347258816281942
  • 45.48059516716245
  • 90





Refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Practice at:
http://jsbin.com

No comments:

Post a Comment