JavaScript basic learning series fourteen: NaN

Dividing any number by 0 usually results in an error in other languages, aborting line 12 of code. But in ECMAScript, division by 0, +0, or 0 returns NaN.

console.log(0/0);    // NaN
console.log(-0/+0);  // NaN

If the numerator is a non-zero value and the denominator is signed 0 or unsigned 0, Infinity or -Infinity is returned:

console.log(5/0);   // Infinity
console.log(5/-0);  // -Infinity

NaN has several unique properties. First, any operation involving NaN always returns NaN (such as NaN/10), which can be a problem when calculating multiple steps in a row. Second, NaN is not equal to any value including NaN.

    console.log(NaN == NaN); // false

For this purpose, ECMAScript provides the isNaN() function. This function receives a parameter, which can be of any data type, and then determines whether the parameter is "not a numeric value". After passing a value to isNaN(), the function attempts to convert it to a numeric value.

Certain non-numeric values ​​can be converted directly to numeric values, such as the string "10" or a Boolean value. Any value that cannot be converted to a numeric value will cause this function to return true.

console.log(isNaN(NaN));
console.log(isNaN(10));
console.log(isNaN("10"));
console.log(isNaN("blue"));
console.log(isNaN(true));
// true
// false,10 是数值
// false,可以转换为数值10 // true,不可以转换为数值 // false,可以转换为数值1

2. Numeric conversion:

Convert non-numeric values ​​to numeric values: Number(), parseInt(), and parseFloat(). Number() is a conversion function that can be used for any data type. The latter two functions are mainly used to convert strings into numerical values. For the same parameters, these three functions perform different operations.

The Number() function performs conversion based on the following rules.

  • Boolean value, true is converted to 1, false is converted to 0.
  • Numeric value, returned directly.
  • null, returns 0.
  • undefined, returns NaN.
  • String, the following rules apply.
  • If the string contains numeric characters, including numeric characters preceded by plus or minus signs, it is converted to a decimal value. Therefore, Number("1") returns 1, Number("123") returns 123, and Number("011") returns 11 (ignoring the leading zero).
  • If the string contains a valid floating point value in the format such as "1.1", it is converted to the corresponding floating point value (again, leading zeros are ignored).
  • If the string contains a valid hexadecimal format such as "0xf", it is converted to the decimal integer value corresponding to the hexadecimal value.
  • If it is an empty string (contains no characters), 0 is returned.
  • If the string contains characters other than those described above, NaN is returned.
  • Object, call the valueOf() method, and convert the returned value according to the above rules. If the conversion result is NaN, call the toString() method, and then convert according to the rules for converting strings.

    let num1 = Number("Hello world!");  // NaN
1 2
 let num2 = Number("");
let num3 = Number("000011");
let num4 = Number(true);
// 0 // 11 // 1

Considering that using the Number() function to convert a string is relatively complicated and a bit unconventional, you can usually use the parseInt() function first when you need to get an integer. The parseInt() function focuses more on whether the string contains a numeric pattern. The leading spaces in the string are ignored, and conversion starts from the first non-space character.

If the first character is not a numeric character, a plus sign, or a minus sign, parseInt() immediately returns NaN. This means that an empty string will also return NaN (unlike Number(), which returns 0).

If the first character is a numeric character, a plus sign, or a minus sign, the test continues for each character in turn until the end of the string or a non-numeric character is encountered. For example, "1234blue" will be converted to 1234, because "blue" will be completely ignored. Similarly, "22.5" is converted to 22 because the decimal point 6 is not a valid integer character.

Guess you like

Origin blog.csdn.net/wanmeijuhao/article/details/135439186