0

I believe I have found cases where I need to check for both undefined and null for a javascript object as follows:

if (x !== undefined && x != null && x.length > 0) {...}

However, in a recent upgrade of JetBrains tools, it tells me that this is sufficient

if (x != undefined && x.length > 0) {...}

My question is, I'm just wanting to insure that a string "x" has a length of non-zero and is not undefined or null (with the least amount tests).

Thoughts?

gdoron
  • 147,333
  • 58
  • 291
  • 367
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188
  • 1
    If you want a string of length > 0, check that `typeof x === 'string' && x.length > 0`. – Matt Aug 24 '13 at 18:59
  • Actually, I'm not sure what you're asking here, – gdoron Aug 24 '13 at 19:00
  • What do you believe in? – Mmmh mmh Aug 24 '13 at 19:04
  • @AurélienOoms, **World Peace**, if you pointed the question to me. ``) – gdoron Aug 24 '13 at 19:05
  • possible duplicate of [In Javascript, is myObject == null a valid way to handle checking for undefined as well?](http://stackoverflow.com/questions/18137996/in-javascript-is-myobject-null-a-valid-way-to-handle-checking-for-undefined) (since the answer is yes, it works the other way round as well) – Bergi Aug 24 '13 at 19:09

7 Answers7

5

in javascript

undefined == null // true
undefined === null // false

so checking with == for undefined makes the == check for null redundant.

gdoron
  • 147,333
  • 58
  • 291
  • 367
2

Try

if (x && x.length)

as undefined, null and 0 are all falsy values.

EDIT: as you seem to know that x should be a string, you can also just use if (x) as an empty string is also falsy.

Dennis
  • 14,210
  • 2
  • 34
  • 54
2

Checking if foo === undefined will trigger the error foo is not defined. See variable === undefined vs. typeof variable === "undefined"

The existential operator in CoffeeScript compiles to

typeof face !== "undefined" && face !== null

Edit:

Matt's comment is better if you only want to check for strings:

typeof x === 'string' && x.length > 0
Community
  • 1
  • 1
Wex
  • 15,539
  • 10
  • 64
  • 107
2

You can use _.isNull from Underscore a JavaScript library provides a whole mess of useful functional programming helpers.

_.isNull(object)

Returns true if the value of object is null.

_.isNull(null);
=> true
_.isNull(undefined);
=> false
Community
  • 1
  • 1
Alaeddine
  • 1,571
  • 2
  • 22
  • 46
2

This is what I use and it's the most concise. It covers: undefined, null, NaN, 0, "" (empty string), or false. We can therefore say that "object" is truthy.

if(object){
    doSomething();
}
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
0

Try this

 if (!x) {
  // is emtpy
}
iJade
  • 23,144
  • 56
  • 154
  • 243
  • The case I'm almost always looking for and I'd like to have a really good "always use" pattern is Is it a non null, non empty string – Peter Kellner Aug 24 '13 at 19:45
0

To check against null AND undefined AND "empty string" you can just write

if(!x) {
   // will be false for undefined, null and length=0
}

BUT you need to be sure that your variable is defined! Otherwise this will cause an error.

If you are checking for a value in an object (e.g. the window object), you can always use that. E.g. for checking for localStorage support:

var supports = {
    localStorage: !!window.localStorage
}
jukempff
  • 965
  • 6
  • 12