es6 Learning Notes - Extension of Numbers
There are some unused or unsupported ones that are not recorded. In general, this article is just a memo.
Number.isFinite(), Number.isNaN()
Number.isFinite() is used to check whether a value is finite.
console.log(Number.isFinite(15)); // true console.log(Number.isFinite(0.8)); // true console.log(Number.isFinite(NaN)); // false console.log(Number.isFinite(Infinity)); // false console.log(Number.isFinite(-Infinity)); // false console.log(Number.isFinite('foo')); // False, the string is also false console.log(Number.isFinite('15')); // false console.log(Number.isFinite(true)); // False, Boolean value is also false
Number.isNaN() is used to check whether a value is NaN.
console.log(Number.isNaN(NaN)) // true console.log(Number.isNaN(15)) // false console.log(Number.isNaN('15')) // false console.log(Number.isNaN(true)) // false console.log(Number.isNaN(9/NaN)) // true console.log(Number.isNaN('true'/0)) // true console.log(Number.isNaN('true'/'true')) // true
The two new methods are only valid for numerical values, and all non-numerical methods return false.
parseInt() and parseFloat()
ES6 ports the global methods parseInt() and parseFloat() onto Number objects, and the behavior remains unchanged.
// The way to write ES5 parseInt('12.34') // 12 parseFloat('123.45#') // 123.45 // The way to write ES6 Number.parseInt('12.34') // 12
Number.isInteger()
Number.isInteger() is used to determine whether a value is an integer. It should be noted that within JavaScript, integers and floating-point numbers are stored in the same way, so 3 and 3.0 are considered to be the same value.
console.log(Number.isInteger(25)) // true console.log(Number.isInteger(25.0)) // true console.log(Number.isInteger(25.1)) // false console.log(Number.isInteger("15")) // false console.log(Number.isInteger(true)) // false
Number.EPSILON
ES6 adds a minimal constant Number.EPSILON to the Number object to set an error range for floating-point computation.
If this error can be less than Number.EPSILON, we can assume that the correct result has been obtained.
0.1 + 0.2 // 0.30000000000000004 0.1 + 0.2 - 0.3 // 5.551115123125783e-17 5.551115123125783e-17.toFixed(20) // '0.00000000000000005551'// Infinitely close to 0 Number.EPSILON.toFixed(20) // '0.00000000000000022204'// / As long as it's smaller than that, we think he's right. function withinErrorMargin (left, right) { return Math.abs(left - right) < Number.EPSILON; } console.log(withinErrorMargin(0.1 + 0.2, 0.3)) // true console.log(withinErrorMargin(0.2 + 0.2, 0.3)) // false
Security integers and Number.isSafeInteger()
JavaScript can accurately represent integers ranging from - 2 ^ 53 to 2 ^ 53 (excluding two endpoints), beyond which the value cannot be accurately represented.
console.log(Math.pow(2, 53)); // 9007199254740992 console.log(Number.MAX_SAFE_INTEGER === Math.pow(2, 53) + 1)//false console.log(Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1) //true, equal to a number less than 1 than the maximum range
Similarly, Number.MAX_SAFE_INTEGER can be used for reference.
Number.isSafeInteger() is used to determine whether an integer falls within this range.
console.log(Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1)) // false console.log(Number.isSafeInteger(Number.MIN_SAFE_INTEGER)) // true console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER)) // true console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1)) // false
Extension of Math Objects
Math.trunc
The Math.trunc method is used to remove the decimal part of a number and return the integer part.
console.log(Math.trunc(4.1)) // 4 console.log(Math.trunc(4.9)) // 4 console.log(Math.trunc(-4.1)) // -4 console.log(Math.trunc(-4.9)) // -4 console.log(Math.trunc(-0.1234)) // -0
Math.sign()
Math.sign method is used to determine whether a number is positive, negative or zero.
console.log(Math.sign(-5)) // -1 console.log(Math.sign(5)) // 1 console.log(Math.sign(0)) // 0 console.log(Math.sign(-0)) // -0 console.log(Math.sign(NaN)) // NaN console.log(Math.sign('foo')); // NaN console.log(Math.sign()); // NaN
Reference: