0

Assuming the foo is declared as a non-primitive type variable, but might be uninitialized, is there any possibility to use on non-primitive object comparison as this:

if (foo) {
...
}

or is it better to do it the longer way like this?

// not using "===" here on purpose, because the variable is non-primitive, so the value can't be 0 or empty string
if (foo != null) {
...
}

If it is not equal, then why.

Andree
  • 1,159
  • 2
  • 17
  • 32
  • 1
    Assuming `foo` is an object, `if(foo)` will pass for any object. – Niet the Dark Absol Sep 18 '14 at 12:10
  • 1
    Man, `null` is primitive. The test will always pass under your assumption. – freakish Sep 18 '14 at 12:11
  • 1
    Check out [Truthy & Falsey](http://james.padolsey.com/javascript/truthy-falsey/). – Greg Burghardt Sep 18 '14 at 12:12
  • Yes, it is, but I'm talking about the variable, not about the null. If the variable is not initialized, I need to find it out and so I make a comparison to null. – Andree Sep 18 '14 at 12:12
  • 2
    This is the exact opposite, I guess you meant if (foo != null) – Adam Sinclair Sep 18 '14 at 12:13
  • If you want to find out if a var is not initialized you should better use != undefined instead of != null – treeno Sep 18 '14 at 12:14
  • @Andree If a variable is not defined then it is `undefined` which again **is** primitive. You need to understand what you are talking about and be precise. – freakish Sep 18 '14 at 12:14
  • @treeno: Those are functionally identical. – cookie monster Sep 18 '14 at 12:14
  • @AdamSinclair Yes, sorry. Corrected. – Andree Sep 18 '14 at 12:15
  • @cookie mosnter undefined and null are fucntionally identical? what do you mean by that? I thought there would be a distinct difference. UNdefined means, that var itself does not exist. null means the var exists but it has no value – treeno Sep 18 '14 at 12:17
  • @freakish Ok, but I need to find out, if it is initialized. Considering it as initialized, it is non-primitive, if it isn't of course it is primitive. – Andree Sep 18 '14 at 12:17
  • @treeno: when you compare using `==` or `!=`, the comparison to `null` and `undefined` behave identically. They will always come up with the same result. – cookie monster Sep 18 '14 at 12:17
  • ...and `undefined` doesn't mean the var doesn't exist. If it doesn't exist, you'll get a ReferenceError when trying to read it. – cookie monster Sep 18 '14 at 12:19
  • @cookie monster because of type coercion? – treeno Sep 18 '14 at 12:19
  • @cookie monster what does it mean then? – treeno Sep 18 '14 at 12:20
  • What exactly does *"not initalized"* mean, is it declared ? – adeneo Sep 18 '14 at 12:20
  • @treeno: Yes, using the Abstract Equality Comparison Algorithm, which is what those operators use, the values of `null` and `undefined` are effectively the same. They compare to each other as well. `null == undefined; // true` – cookie monster Sep 18 '14 at 12:20
  • ...people think of `undefined` as meaning "existing but not initialized", and they think of `null` as meaning "has been set to an empty value". These are conventions. I think if they did it again, the language wouldn't have both values. – cookie monster Sep 18 '14 at 12:22
  • http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined – adeneo Sep 18 '14 at 12:26

2 Answers2

2

By "non-primitive type" you mean object? Yes, all objects are truthy, so it is enough to distinguish them via if (foo) from any falsy values that the foo variable might contain, such as the primitive values undefined and null.

However, notice that you cannot "declare a variable as a non-primitive type" in JavaScript, so you must ensure by other means that it never has a truthy primitive value. If you could ensure that it always contains an object, you wouldn't need the condition at all.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

In my experience, I have found it to be safest to specify exactly what you want in boolean expressions and never rely on type coercion.

If you don't want it to be equal to 0 or null or empty string, then:

if (foo !== 0 && foo !== '' && foo !== null) {
  /* note that if foo = undefined, this will still get executed. */
}

Some libraries like Underscore or a framework like Angular have convenience functions for things like that, so you can do:

if (_.isObject(foo) === true) {
  /* execute this */
}
M.K. Safi
  • 6,560
  • 10
  • 40
  • 58