The difference between != and =!
Posted on December 31st, 2008 in JavaScript | No Comments »
I was helping a friend troubleshoot some of her code today and found the culprit to be a typo in one of her conditional statements:
// var USERID = 12345; if (USERID =! null){ // code }
Her expression was always resolving to true because she had inverted her != (“not-null”) check which was actually assigning her variable to the return of a statement evaluation. Her mistake would be clearer to understand formatted as such:
// var USERID = 12345; if (USERID = !null){ // code }
Since the inverse of false, null, and NaN is true her conditional statement (also an assignment) would always return true.
Which lead me to thinking, the not operator is a great way to write succinct code. In the right context, using the not operator in the right side of an assignment isn’t a bad idea.