2024-05-06 20:40:05 +00:00
|
|
|
---
|
2024-10-30 17:04:36 +00:00
|
|
|
date: 20200923
|
2024-05-06 20:40:05 +00:00
|
|
|
id: 1accac75-7a5c-4847-977a-f0ae63454820
|
|
|
|
title: 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.
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
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)
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
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:
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
console.log(Math.cbrt(8)) // 2
|
|
|
|
console.log(Math.cbrt(27)) // 3
|
|
|
|
```
|