数值的表示
二进制表示法新写法: 前缀 0b 或 0B 。
console.log(0b11 === 3); // true
console.log(0B11 === 3); // true
八进制表示法新写法: 前缀 0o 或 0O 。
console.log(0o11 === 9); // true
console.log(0O11 === 9); // true
常量
Number.EPSILON 属性表示 1 与大于 1 的最小浮点数之间的差。
它的值接近于 2.2204460492503130808472633361816E-16,或者 2-52。
isFinite
Number.isFinite() 用于检查一个数值是否为有限的( finite ),即不是 Infinity
在全局的 isNaN() 中,以下皆返回 true,因为在判断前会将非数值向数值转换
而 Number.isNaN() 不存在隐式的 Number() 类型转换,非 NaN 全部返回 false
注意:Number.isFinate,Number.isNaN 没有隐式的 Number() 类型转换,所有非数值都返回 false
从全局移植到 Number 对象的方法
Number.parseInt()
Number.parseFloat()
Math 对象的扩展
Math.cbrt() 用于计算一个数的立方根。
Math.cbrt(1); // 1
Math.cbrt(0); // 0
Math.cbrt(-1); // -1
// 会对非数值进行转换
Math.cbrt('1'); // 1
// 非数值且无法转换为数值时返回 NaN
Math.cbrt('hhh'); // NaN
Math.imul() 两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数。
Math.hypot() 用于计算所有参数的平方和的平方根。
Math.hypot(3, 4); // 5
// 非数值会先被转换为数值后进行计算
Math.hypot(1, 2, '3'); // 3.741657386773941
Math.hypot(true); // 1
Math.hypot(false); // 0
// 空值会被转换为 0
Math.hypot(); // 0
Math.hypot([]); // 0
// 参数为 Infinity 或 -Infinity 返回 Infinity
Math.hypot(Infinity); // Infinity
Math.hypot(-Infinity); // Infinity
// 参数中存在无法转换为数值的参数时返回 NaN
Math.hypot(NaN); // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot({}); // NaN
Math.clz32() 用于返回数字的32 位无符号整数形式的前导0的个数。
Math.trunc() 用于返回数字的整数部分。
Math.trunc(12.3); // 12
Math.trunc(12); // 12
// 整数部分为 0 时也会判断符号
Math.trunc(-0.5); // -0
Math.trunc(0.5); // 0
// Math.trunc 会将非数值转为数值再进行处理
Math.trunc("12.3"); // 12
// 空值或无法转化为数值时时返回 NaN
Math.trunc(); // NaN
Math.trunc(NaN); // NaN
Math.trunc("hhh"); // NaN
Math.trunc("123.2hhh"); // NaN
Math.fround() 用于获取数字的32位单精度浮点数形式。
Math.sign() 判断数字的符号(正、负、0)。
Math.expm1() 用于计算1 + x 的自然对数,即 Math.log(1 + x) 。
Math.log10(x) 用于计算以 10 为底的 x 的对数。
Math.log2() 用于计算 2 为底的 x 的对数。
Math.sinh(x): 用于计算双曲正弦。
Math.cosh(x): 用于计算双曲余弦。
Math.tanh(x): 用于计算双曲正切。
Math.asinh(x): 用于计算反双曲正弦。
Math.acosh(x): 用于计算反双曲余弦。
Math.atanh(x): 用于计算反双曲正切。
指数运算符:**
1 ** 2; // 1
// 右结合,从右至左计算
2 ** 2 ** 3; // 256
// **=
let exam = 2;
exam ** = 2; // 4
网友评论