John Cooper John Cooper - 22 days ago 4
Javascript Question

Using underscore.js for null checking

var name = "someName";
if(name !=null) {
// do something

  1. I am right now using, how would i do
    the same using

  2. Does it give any slight improvement in terms of performance for such


In underscore, you can use

if(!_.isNull(name)) {}

and in plain Javascript, you should use

if(name !== null) {}

You should avoid the loose inequality operator != because it does type coercion and undefined != null will return false.

Using plain Javascript is slightly faster because it doesn't have to invoke a function, but it will be imperceptible and it should hardly be a consideration.

I don't have a strong preference either way as far as readability goes, but it seems a little excessive and verbose to call a library function for such a simple check.