JavaScript : relaxing equality

Recently I received a pull request for an issue in cordova.exec on the Windows platform. It seems that a plugin result of type NO_RESULT would not be handled correctly, because NO_RESULT was actually defined as 0, and we were loosely checking along the lines of if(pluginResult.status){...} The pull request added logic to check for undefined like this :

if(pluginResult.status !== undefined){...}

On the surface it looks fine, and status of 0 will still be handled, however, because it is using strict equality comparison, and values of null are possible, and additional check was added :

if(pluginResult.status !== undefined && pluginResult.status !== null){...}

A verbose solution, but it does do the trick.

Ultimately, this is a case where the strict equality check is not helping us. After writing a few tests to ensure I was not misguided, I ended up writing :

if(pluginResult.status != null){...}

The basic equality comparison here will have the same result for undefined and null, so there is no need to test for both. Many times I hear blanket statements like 'always use strict equality' although I think is more important to simply know how and when it applies.

Here are the tests I wrote to verify the behaviour of various comparisons: Note that I also considered using Object.hasOwnProperty as an alternative way of checking if the object had a value, 0 included.

// Loose equality ({status:undefined}).status != null // false ({status:undefined}).status != undefined // false ({status:undefined}).status != 0 // true

({status:null}).status != undefined // false ({status:null}).status != null // false ({status:null}).status != 0 // true

({status:0}).status != null // true ({status:0}).status != undefined // true ({status:0}).status != 0 // false

// Strict equality ({status:undefined}).status !== null // true ({status:undefined}).status !== undefined // false ({status:undefined}).status !== 0 // true

({status:null}).status !== null // false ({status:null}).status !== undefined // true ({status:null}).status !== 0 // true

({status:0}).status !== null // true ({status:0}).status !== undefined // true ({status:0}).status !== 0 // false

// another way ({status:undefined}).hasOwnProperty('status') // true ({status:null}).hasOwnProperty('status') // true ({status:0}).hasOwnProperty('status') // true ({}).hasOwnProperty('status') // false