In the JavaScript world, whether it's a function, an array, an object, or a string, a number, or a Boolean type, there are always a few maverick in these data types, and then there are some nasty bugs that are dispensable.
The reason these annoying bugs occur is because of the errors caused by comparisons, and the most difficult to detect errors.
Here we have collated the comparisons of these special data types. directly on the code.
Before the code, popularize a knowledge:
= = Content comparison, data types are not compared.
= = = Content comparison, data types are also compared.
On the code:
= = Comparison code:
//"" Null undefined NaN 0Console.log ("\" \ "==\" \ ":"); Console.log (""=="");//falseConsole.log ("\" \ "==null:"); Console.log (""==NULL);//falseConsole.log ("\" \ "==undefined:"); Console.log ("==undefined");//falseConsole.log ("\" \ "==nan:"); Console.log ("==nan");//falseConsole.log ("\" \ "==number.nan:"); Console.log ("==number.nan");//falseConsole.log ("\" \ "==0:"); Console.log ("==0");//trueConsole.log ("Null==\" \ ":"); Console.log (NULL==""); Console.log ("Null==null:"); Console.log (NULL==NULL); Console.log ("Null==undefined:"); Console.log (NULL==undefined); Console.log ("Null==nan:"); Console.log (NULL==NaN); Console.log ("Null==number.nan:"); Console.log (NULL==Number.NaN); Console.log ("Null==0:"); Console.log (NULL==0); Console.log ("Undefined==\" \ ":"); Console.log (Undefined==""); Console.log ("Undefined==null:"); Console.log (Undefined==NULL); Console.log ("Undefined==undefined:"); Console.log (Undefined==undefined); Console.log ("Undefined==nan:"); Console.log (Undefined==NaN); Console.log ("Undefined==number.nan:"); Console.log (Undefined==Number.NaN); Console.log ("Undefined==0:"); Console.log (Undefined==0); Console.log ("Nan==\" \ ":"); Console.log (NaN==""); Console.log ("Nan==null:"); Console.log (NaN==NULL); Console.log ("Nan==undefined:"); Console.log (NaN==undefined); Console.log ("Nan==nan:"); Console.log (NaN==NaN); Console.log ("Nan==number.nan:"); Console.log (NaN==Number.NaN); Console.log ("Nan==0:"); Console.log (NaN==0); Console.log ("0==\" \ ":"); Console.log (0== ""); Console.log ("0==null:"); Console.log (0==NULL); Console.log ("0==undefined:"); Console.log (0==undefined); Console.log ("0==nan:"); Console.log (0==NaN) Console.log ("0==number.nan:"); Console.log (0==Number.NaN); Console.log ("0==0:"); Console.log (0==0);
= = Comparison Result:
= = = Comparison code:
//= = Content comparison, data types are not compared. //= = = Content comparison, data types are also compared. //"" Null undefined NaN 0Console.log ("\" \ "===\" \ ":"); Console.log (""==="");//falseConsole.log ("\" \ "===null:"); Console.log (""===NULL);//falseConsole.log ("\" \ "===undefined:"); Console.log ("===undefined");//falseConsole.log ("\" \ "===nan:"); Console.log ("===nan");//falseConsole.log ("\" \ "===number.nan:"); Console.log ("===number.nan");//falseConsole.log ("\" \ "===0:"); Console.log ("===0");//trueConsole.log ("Null===\" \ ":"); Console.log (NULL===""); Console.log ("Null===null:"); Console.log (NULL===NULL); Console.log ("Null===undefined:"); Console.log (NULL===undefined); Console.log ("Null===nan:"); Console.log (NULL===NaN); Console.log ("Null===number.nan:"); Console.log (NULL===Number.NaN); Console.log ("Null===0:"); Console.log (NULL===0); Console.log ("Undefined===\" \ ":"); Console.log (Undefined===""); Console.log ("Undefined===null:"); Console.log (Undefined===NULL); Console.log ("Undefined===undefined:"); Console.log (Undefined===undefined); Console.log ("Undefined===nan:"); Console.log (Undefined===NaN); Console.log ("Undefined===number.nan:"); Console.log (Undefined===Number.NaN); Console.log ("Undefined===0:"); Console.log (Undefined===0); Console.log ("Nan===\" \ ":"); Console.log (NaN===""); Console.log ("Nan===null:"); Console.log (NaN===NULL); Console.log ("Nan===undefined:"); Console.log (NaN===undefined); Console.log ("Nan===nan:"); Console.log (NaN===NaN); Console.log ("Nan===number.nan:"); Console.log (NaN===Number.NaN); Console.log ("Nan===0:"); Console.log (NaN===0); Console.log ("0===\" \ ":"); Console.log (0=== ""); Console.log ("0===null:"); Console.log (0===NULL); Console.log ("0===undefined:"); Console.log (0===undefined); Console.log ("0===nan:"); Console.log (0===NaN) Console.log ("0===number.nan:"); Console.log (0===Number.NaN); Console.log ("0===0:"); Console.log (0===0);
= = = Comparison results:
typeof () and isNaN () code:
Console.log (typeof""); Console.log (typeof NULL); Console.log (typeofundefined); Console.log (typeofNaN); Console.log (typeof0); //"" Null undefined NaN 0Console.log ("IsNaN (\" \ ")"); Console.log (IsNaN ("")); Console.log ("IsNaN (NULL)"); Console.log (IsNaN (NULL)); Console.log ("IsNaN (undefined)"); Console.log (IsNaN (undefined)); Console.log ("IsNaN (NaN)"); Console.log (IsNaN (NaN)); Console.log ("IsNaN (0)"); Console.log (IsNaN (0));
typeof () and isNaN () results:
Conclusion:
Double equals 0== "" is true, null==undefined is true other than Nan equals it is true, all that is left is false
Three equals to being true except Nan, and all that's left is false.
"" is a string type
Null is an object type
Undefined is the undefined type
0 and Nan are number types
Above pure hand dozen, if have shortcomings, also hope to point out, in under grateful.
2016.8.01 "" Null undefined NaN 0 Special comparison is true or false