16
var name = "someName";
if(name !=null) {
   // do something
}
  1. I am right now using http://underscorejs.org/#isNull, how would i do the same using underscore.js
  2. Does it give any slight improvement in terms of performance for such functions.
John Cooper
  • 7,343
  • 31
  • 80
  • 100

4 Answers4

26

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.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
1

In underscore.js you must write this to achieve that functionality.

var name = "someName";
if(!(_.isNull(name ))) {
   // do something
}

In underscore.js function isNull is written like this

_.isNull = function(obj) {
    return obj === null;
  };

So the difference is using == in your code and === in underscore.js.For more details about that difference you can look in this question.

Which equals operator (== vs ===) should be used in JavaScript comparisons?

P.S. I will suggest to write your own condition instead of using any library in such simple place.

var name = "someName";
if(name !== null)) {
   // do something
}
Community
  • 1
  • 1
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
0

Well your original code is flawed because if name is an empty string, false, the number 0 or any other falsy value then it will be considered null as far as your test is concerned.

As a general rule, calling ANY function is an overhead that should be avoided if possible. In this case, calling a function just to test if a value is null, when you could very easily just write if( name === null), is just stupid, IMO...

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    Just for people that are going to come to this question, no "", false and 0 will not match null, only undefined and null will match null when using == null. On the other side, when checking for == false (or !something) THEN it'll also match null, undefined, 0, "", "0" and "false" – XIU Dec 12 '12 at 19:38
  • 1
    @XIU Thank you. `"0" == false` is always the one that gets me. What a crock… – wchargin Nov 02 '15 at 19:51
0
if ((!(_.isUndefined(data)) || _.isEmpty(data))) {
//Valid Data 
}else {//Not Valid}
Ashutosh Soni
  • 191
  • 1
  • 4
  • 3
    Please elaborate on your answer. – Shawn Oct 04 '16 at 18:05
  • Hi Shawn In case we need to perform some operation on json data /object so we can simple user he underscore library and perform 2 check first isUndefined (data) must be false and Is empty data is also valid so we jusy put condition for verify if ( !(_.isUndefined(data)) || _.isEmpty(data) ) { //Valid Data json data //TODO WORK } else { //Not Valid json result } – Ashutosh Soni Jan 14 '20 at 18:20