Be careful when using instanceof

If you intend to throw an error if an object is not an instanof a specific object, be careful of this mistake that's easy to make.

 

if( !someVar instanceof SomeObject){  //WRONG!
throw new Error("....");
}

 If someVar is null or undefined then your error will not be thrown.

 

This is better:

 

if( !(someVar instanceof SomeObject)){  
throw new Error("....");
}