It is inevitable to deal with numbers in programming. However, operations on numbers in Javascript may be confusing in some cases. For example, parseint is a typical example.
1. parseint hexadecimal Problem
Parseint is a very common method in Javascript. It is used to convert a numeric string to a numeric integer. This function is generally used to avoid any problems, but in some cases, for example, the string to be converted is "08", "09 ". The following code shows the different returned results when parseint processes the string "08. The reason for this is that it is rare to note that the parseint method actually has two parameters: the first is the string to be converted to a number, and the second is the hexadecimal value of the conversion, if the second parameter is not provided, the string prefixed with '0x 'is treated as a hexadecimal notation, and the string prefixed with '0' is treated as an octal notation. All other strings are treated as decimal. This causes the parseint ("08") result to be 0, while the parseint ("8") result to be 8. The solution is simple: specify parameters for this method.
var t_1_a = "08";parseInt(t_1_a); // 0parseInt(t_1_a,10); // 8
2. 0 divided by 0 and 1 divided by 0
When performing Division operations, if you do not know the two numbers involved in the calculation, you may encounter situations similar to 0/0 and 1/0. Thanks to the math, he made us know n/0 (n! = 0) is a limit number. In JavaScript, when n is greater than 0, the operation result of this expression is infinity. If n is less than 0, the result is-infinity. Don't think that I wrote a word here. Infinity is a member of the Global object. In fact, its data type is still "number ". When N is 0, that is, 0/0, the result of this operation is Nan, which is a special guy. To avoid this problem, one method is to check the number involved in the operation, and the other method is to check the operation result.
1/0 // Infinity0/0 // NaN
3. Number Detection: isnan and isfinite
We have just recognized a situation where infinity and Nan may occur. However, in practice, there are many examples of such results. For example, in a user form, only numbers can be entered for one field, how to detect invalid user input, ensure effective calculation, and return to the user. Let's talk about Nan first. It is a little more complex than infinity. Nan is used to represent special values of non-numeric values. Like infinity, Nan is also a member of the Global object.Nan is not equal to any value, including its ownThis denies the possibility that we use "=" to detect Nan, andThe result of typeof Nan is also "number"This makes it impossible to distinguish it from conventional numbers.
typeof NaN; // number"test" == NaN; // false5 == NaN; // falseNaN == NaN; // false
Fortunately, JavaScript provides a method named isnan, which accepts a unique parameter and returns whether the parameter is a reserved value Nan, the code below has almost succeeded in distinguishing between numbers and non-numbers. The only thing to note is that the last line, the empty string is also returned with false, which indicates that JavaScript considers it a legal number, this is because the empty string is implicitly converted to the number 0, which tells us that it is necessary to disable the submission of null characters or to verify the Length attribute.
isNaN(NaN); // trueisNaN("5"); // falseisNaN("5a"); // trueisNaN(""); // false
The verification of infinity is much simpler, because it can use equal and full operations, or isfinite detection. The isfinite method also accepts only unique parameters to check whether the provided numbers are limited.
1/0 == Infinity // trueisFinite(1/0); // false
4. Use the "+" operator to convert a string to a number
Add a plus sign before the variable to be converted to a number. This is the most calm and calm Method for transforming the numeric type.
var a = "5";var b = 6;a + b; // "56"+(a) + b; // 11
5. The story of the number 0
Javascript is so flexible that sometimes it cannot tell whether 0 is zero or not. In fact, 0 can represent either a number 0 or a string with a length of 0, or a Boolean value false, therefore, it is very correct to use the "=" full equal sign when you need to clearly distinguish them.
Most experienced front-end programmers advocate full operators and specify data types as much as possible to avoid errors such as the above, at the same time, it will avoid unnecessary data type conversion during Javascript internal processing. The advantage of this is that it can make the code run faster.
0 == false; // true0 == "" // true0 === false; // false0 === "" // false