-
-
Save zhuzhuaicoding/1433092 to your computer and use it in GitHub Desktop.
Use === and !==
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use === and !== | |
// It is often better to use the === and !== operators | |
// rather than == and != operators. The reason for this | |
// is that === and !== (also known as the identity operators) | |
// check for the type as well as the value when being compared | |
// whereas the == and != will try to coerce the two values | |
// into the same type before the comparison is made, which | |
// may lead to some very unexpected results. | |
// Unexpected Comparisons using the == Operator | |
console.log( "\nUnexpected Comparisons using the == Operator" ); | |
console.log( "0 == ''", 0 == '' ); //true | |
console.log( "0 == '0'", 0 == '0' ); //true | |
console.log( "false == '0'", false == '0' ); //true | |
console.log( "null == undefined", null == undefined ); //true | |
console.log( "' \t\r\n ' == 0", ' \t\r\n ' == 0 ); //true | |
// Expected Comparions using the === Operator | |
console.log( "\nExpected Comparions using the === Operator" ); | |
console.log( "0 === ''", 0 === '' ); //false | |
console.log( "0 === '0'", 0 === '0' ); //false | |
console.log( "false === '0'", false === '0' ); //false | |
console.log( "null === undefined", null === undefined ); //false | |
console.log( "' \t\r\n ' === 0", ' \t\r\n ' === 0 ); //false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment