Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Created September 21, 2010 16:26
Show Gist options
  • Save elijahmanor/589968 to your computer and use it in GitHub Desktop.
Save elijahmanor/589968 to your computer and use it in GitHub Desktop.
Use === and !==
// 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