最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Checking JavaScript object for null subtelty - Stack Overflow

programmeradmin2浏览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?

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?

Share Improve this question edited Aug 24, 2013 at 18:58 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Aug 24, 2013 at 18:54 Peter KellnerPeter Kellner 15.5k28 gold badges112 silver badges201 bronze badges 5
  • 1 If you want a string of length > 0, check that typeof x === 'string' && x.length > 0. – Matt Commented Aug 24, 2013 at 18:59
  • Actually, I'm not sure what you're asking here, – gdoron Commented Aug 24, 2013 at 19:00
  • What do you believe in? – Mmmh mmh Commented Aug 24, 2013 at 19:04
  • @AurélienOoms, World Peace, if you pointed the question to me. ``) – gdoron Commented Aug 24, 2013 at 19:05
  • possible duplicate of In Javascript, is myObject == null a valid way to handle checking for undefined as well? (since the answer is yes, it works the other way round as well) – Bergi Commented Aug 24, 2013 at 19:09
Add a ment  | 

7 Answers 7

Reset to default 5

in javascript

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

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

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

The existential operator in CoffeeScript piles to

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

Edit:

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

typeof x === 'string' && x.length > 0

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.

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

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

Try this

 if (!x) {
  // is emtpy
}

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
}
发布评论

评论列表(0)

  1. 暂无评论