Invalid values in JavaScript come in all shapes and sizes. Sometimes you just want to know if a variable has a valid value so that you can execute the necessary code.

I had a pretty hard time with this, but I found that testing a variable for a ‘Truthy’ value works very well for me. When I say test for a ‘Truthy’ value, I mean a legit value that will return true. See below example:

The example above would return true because myVariable contains a valid value. If myVariable returned one of the following, the above example would return False:

– Undefined
– Null
– <null>
– NaN
– +0
– -0
– Empty String where length is 0.

Hope this helps 🙂