Implicit conversions in JS can usually be converted into corresponding data types through functions such as parseint (), number (), String (), and Boolean (), but usually for simplicity, we use a special notation, such as:
var a = 5;console.log (A + "); Output ' 5 ', turn number type to string type
Here is the string converted to number type Console.log (+ ' + '); Output 56, equivalent to number (a) console.log (-' n '); Output -56console.log (+ "); 0console.log (+ ' 34A '); Output nan Console.log (-' 34A '); Output NaNconsole.log (parseint (' 34A ', 10)); Output 34; parseint The second parameter is best passed in 10, if not, in ES3, the default is converted to 8 binary console.log (+[]);
Turns a string or number into a Boolean value of Console.log (!5); False, this process will first turn 5 to true, then take the non-console.log (!! 5); True, equivalent to Boolean (5) console.log (!! ') 2 '); Trueconsole.log (!! "); Falseconsole.log (!! 0); Falseconsole.log (!! [] ); Trueconsole.log (!! {} ); Trueconsole.log (!! NULL); Falseconsole.log (!! undefined); Falseconsole.log (!! /abc/g ); True
Summarize:
- Any data type and string are added together, resulting in a string.
- Only an empty array (if there is an element in the array, and this element can only be converted to 0), an empty string, a string full of spaces, false will be converted to 0, the other look at the situation
- Only null, undefined, empty string, 0, Nan will be converted to false, all others are true
- If you can make sure that the string is made up of pure numbers, you can save time and effort by converting it to number type in front with "+". (Many of these are written in jquery)
- The judgment statement still uses the = = = to carry on the strict judgment, avoids the unnecessary trouble
So, [] = =! [] Get the true, because the right one! [] will be converted first to false, and then because it is = = and the data type is different on both sides, so the two sides will be converted to number for comparison, the left [] to number is 0, the right false also turns to 0, so equal returns True
One thing to be aware of: null = = undefined the comparison gets true
The above is purely personal summary, if there are errors, please correct me.
JS Implicit conversion Personal Summary