It is well known that null and undefined are unique in Javascript. However the following behavior was not known to me and hence the post. Basically null is coerced to 0 when using >, <, >=, <= operators but not when == operator is used. Quirky at its best :)
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
var counter = null; | |
var something = false; | |
if(something) { | |
counter += 1; | |
} | |
if(counter == 0) { | |
console.log("failed to update"); | |
} | |
if(counter <= 0) { | |
console.log("print again the failure"); | |
} | |
// if something is true this will be printed | |
if(counter > 0) { | |
console.log("successfully updated"); | |
} |