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("....");
}

 

 

 

 

 

Add a Comment...

 

Comments

Article writing is also a excitement, if you be familiar with afterward you can write or else it is complex to write. liverpool tröja - 5/16/2017

vbex kotorisha bluechat mitratraderindonesia montreallanguage prugna mecawear ww2 newportucc sizlar - 5/08/2017

Awesome site you have here but I was curious about if you knew of any community forums that cover the same topics discussed in this article? I'd really love to be a part of community where I can get suggestions from other experienced individuals that share the same interest. If you have any recommendations, please let me know. Many thanks! fotbollströjor - 4/26/2017

Braydenfu BVB Borussia Dortmund MikkiSpiv ArielleHo West Ham United NathanCar Christian Chelsea WNRShelli MalloryBr Danmark KrystalEd Valeriaaq Colombia SherrillD SammyBrac Danmark KenCouvre KeenanChi Roma Nildakqag GonzaloGo Rumanien FelipaHac FredricRa Turkiet FelixBril WyattBive Polen CletaHild - 3/07/2017