Skip to content

Latest commit

History

History
55 lines (50 loc) 路 1.53 KB

File metadata and controls

55 lines (50 loc) 路 1.53 KB

Math Object

Math is a built-in javascript object that provides a collection of properties and methods related to mathematics.
Example: Math.PI, Math.E

馃搻 Math methods

  • round() method is used to round the decimal to nearest integer

console.log(Math.round(3.4)) // 3
  • floor() method is used to reduce the decimal to the highest integer lesser than itself

console.log(Math.floor(3.9)) // 3
  • ceil() method is used to increase the decimal to the lowest integer greater than itself

console.log(Math.ceil(3.22)) // 4
  • trunc() method is used to ignore all the decimal places and keep just the integer

console.log(Math.trunc(3.88)) // 3
  • abs() methods to convert negative to positve number (mod function)

console.log(Math.abs(-2.03)) // 2.03
  • pow() and sqrt() methods

console.log(Math.pow(2,3)) // 8

console.log(Math.sqrt(81)) // 9
  • log() methods returns the logarithm of the number to the base e (= 2.71)

console.log(Math.log(10)) // 2.3
  • Trignometric functions as Math object methods

console.log(Math.sin(0)) // 0

console.log(Math.cos(0)) // 1

console.log(Math.tan(0)) // 0
  • sign() method return -1 for negative, 1 for positive and 0 for zero

console.log(Math.sign(-9)) // -1
console.log(Math.sign(0)) // 0
console.log(Math.sign(9)) // 1

Note

You can assign numbers to variables and pass variables to methods instead.


previous

next