wiki/content/20200923153614-math.md

825 B

date id title
2020-09-23 1accac75-7a5c-4847-977a-f0ae63454820 JavaScript Math library

ES6

.sign(x)

Returns

  • -1 if x is a negative number (including -Infinity).
  • 0 if x is zero4.
  • +1 if x is a positive number (including Infinity).
  • NaN if x is NaN or not a number.
console.log(Math.sign(-8)) // -a
console.log(Math.sign(3)) // 1
console.log(Math.sign(0)) // 0
console.log(Math.sign(NaN)) // NaN
console.log(Math.sign(-Infinity)) // -1
console.log(Math.sign(Infinity)) // 1

.trunc(x)

console.log(Math.trunc(3.1)) // 3
console.log(Math.trunc(3.9)) // 3
console.log(Math.trunc(-3.1)) // 3
console.log(Math.trunc(-3.9)) // 3

.cbrt(x)

Returns the cube root of x:

console.log(Math.cbrt(8)) // 2
console.log(Math.cbrt(27)) // 3