ES6新增的math,Number方法,小编觉得非常不错,需要的朋友参考下吧

ES6新增的math,Number方法,下面总结了一些我觉得有用的

Nunber.isInteger()判断是否为整数,需要注意的是1,和1.0都会被认为是整数


console.log(Number.isInteger(1.0))//true
console.log(Number.isInteger(1))//true
console.log(Number.isInteger("1"))//false
console.log(Number.isInteger("1.1"))//false
登录后复制

Math.sign()判断是正数,负数,还是0


console.log(Math.sign(1))//1
console.log(Math.sign(-1))//-1
console.log(Math.sign(0))//0
console.log(Math.sign(-0))//0
console.log(Math.sign(NaN))//NaN
console.log(Math.sign(undefined))//NaN
console.log(Math.sign(null))//0
登录后复制

Math.cbrt()计算一个数的立方根


console.log(Math.cbrt(8))//2
Math.hypot()返回所有参数的平方和的平方根
console.log(Math.hypot(4,3))//25再开方结果为5
登录后复制

指数运算


console.log(2**2) //4     
console.log(2**3) //8
登录后复制

以上就是JavaScript ES6中新增math,Number方法讲解的详细内容,更多请关注Work网其它相关文章!

09-19 01:32