Js Number
Contents
1. NaN:
Definition:
NaNis a special value in JavaScript, which means “Not a Number”. It mainly occurs when there is an error in parsing a string into a number.NaNis not an independent data type, but a special value. Its data type still belongs tonumber, which can be seen clearly using thetypeofoperator.
typeof NaN // ’number'
- NaN is not equal to any value, including itself.
NaN === NaN // false
- isNaN(): can be used to determine whether a value is NaN.
Note: If a non-numeric value (such as a string) is passed in, type conversion will be performed first and converted to NaN, so this method is not reliable! Before using isNaN(), you need to determine whether the data type is number.
function isRealNaN(x){ return typeof x === “number” && isNaN(x); }
Or make use of the fact that NaN is the only value in JavaScript that is not equal to itself to make a judgment.
function isTrueNaN(x){ return x !== x; }
2. Infinity and isFinite()
Infinity: means “infinity” and is a special value used to represent two scenarios. One is that a positive value is too large, or a negative value is too small to be represented; the other is that a non-0 value is divided by 0 to getInfinity.isFinite(): Returns aBoolean valueto check whether a certain value is anormal value.
If the isFinite function is used on NaN, it also returns false, indicating that NaN is not a normal value.
isFinite(Infinity) // false isFinite(NaN) // false isFinite(“abc”) // false isFinite(undefined) //false isFinite({}) //false
Note that true is returned when the parameters are true, false, null, [], "".
isFinite(true) // true isFinite(false)// true isFinite(null) //true isFinite([]) //true isFinite("") //true isFinite(-1) // true
3. parseInt() and parseFloat()
parseInt()is used to convertstringtointeger;parseFloat()is used to convertstringtofloating point number.
parseInt(‘123’) // 123 parseFloat(‘3.14’) // 3.14
- When a string is converted to an integer or floating point number, it is converted one character at a time. If it encounters a character that cannot be converted into a number, it will not continue and the converted part will be returned.
parseInt(‘15px’) // 15 parseFloat(‘3.14abcd ‘) // 3.14
- If there are spaces before and after the string, the spaces will be automatically removed.
parseInt(’ 81’) // 81 parseFloat(’\t\v\r12.34\n ‘) // 12.34
- If the first character of the string cannot be converted into a number (except for the sign followed by a number),
NaNis returned.
parseInt(‘abc’) // NaN parseInt(’.3’) // NaN parseInt(’’) // NaN parseInt(’+’) // NaN parseInt(’+1’) // 1
- For those numbers that will be automatically converted to
scientific notation, parseInt() will treat the scientific notation representation as a string, thus leading to some strange results. If the string conforms to scientific notation, parseFloat()) will perform the corresponding conversion.
parseInt(1000000000000000000000.5) // 1 // Equivalent to parseInt(‘1e+21’) // 1
parseFloat(‘314e-2’) // 3.14 parseFloat(‘0.0314E+2’) // 3.14
parseInt()can also accept a second parameter (between 2 and 36), indicating the base of the parsed value, and returns the decimal number corresponding to the value. By default, the second parameter ofparseIntis 10, that is, the default is decimal to decimal.
parseInt(‘1000’) // 1000 // Equivalent to parseInt(‘1000’, 10) // 1000 parseInt(‘1000’, 2) // 8 parseInt(‘1000’, 6) // 216 parseInt(‘1000’, 8) // 512
- If the string contains meaningless characters for the specified base, starting from the highest bit, only values that can be converted will be returned. If the highest bit cannot be converted,
NaNis returned directly.
parseInt(‘1546’, 2) // 1 parseInt(‘546’, 2) // NaN
- If the first parameter of
parseInt()is not a string, it will be converted to a string first. This can lead to some surprising results.
parseInt(0x11, 36) // 43 // Equivalent to parseInt(‘17’, 36) parseInt(String(0x11), 36)
- If the parameter is not a string, or the first character of the string cannot be converted to a floating point number,
parseFloat()returnsNaN.
parseFloat([]) // NaN parseFloat(‘FF2’) // NaN parseFloat(’’) // NaN
4. Forced type conversion function: Number()
Number(): can convert any type of value into a numerical value.
Number()converts a string into a numerical value, which is much stricter thanparseInt(). Basically, as long as one character cannot be converted to a numeric value, the entirestringwill be converted toNaN.
parseFloat(‘123.45#’) // 123.45 Number(‘123.45#’) // NaN
- If the parameter of
Number()isnull,false,'',[], the result is0; if it istrue, the result is1. If the parameters ofparseInt()/parseFloat()are these special values, they will always beNaN.
Number(null) //0 Number("") //0 Number(false) //0 Number([]) //0 Number(true) // 1
parseInt(null) // NaN parseFloat(null) // NaN parseInt("") // NaN parseFloat("") // NaN parseInt(false) // NaN parseFloat(false) // NaN parseInt([]) // NaN parseFloat([]) // NaN parseInt(true) // NaN parseFloat(true) // NaN
Number(), likeparseInt()andparseFloat(), will automatically filter thespacesbefore and after the string.
Number(’\t\v\r12.34\n’) // 12.34
- If the parameter is an object, NaN will be returned unless it is an array containing a single value.
Number({a: 1}) // NaN Number([1, 2, 3]) // NaN Number([5]) // 5