"Falsy" Mishaps in JavaScript

I sometimes see web developers check for falsy values incorrectly in JavaScript. It's easy to get wrong. Here are a few common mistakes. Missing Null if (myVariable === undefined) This is probably the most frequent mistake I see. While this catches undefined properly, it doesn't guard against reference errors when myVariable is null. Unless you're explicitly trying to exclude null from your check, this will result in a null reference error if you try to access a property on myVariable. Missing Undefined if(myVariable === null) I don't see this as often, but it does happen. While this catches null properly, it…

read more...