JavaScript ES6中新增math,Number方法讲解

巴扎黑
巴扎黑 原创
2017-08-08 09:36:32 1314浏览

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方法讲解的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。