views:

388

answers:

7

How to right this syntax correctly:

if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {

...

}

(to check if any of the first 3 variables is null or undefined)

+2  A: 

Use:

if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null || 
    tipoTropaPrioritaria[m] == undefined || troopsCount[m] == undefined || availableTroops[m] == undefined) {
    // ...
}

EDIT: It's probably better to use the === (threequals) operator in this case.

if (tipoTropaPrioritaria[m] === null || troopsCount[m] === null || availableTroops[m] === null || 
    tipoTropaPrioritaria[m] === undefined || troopsCount[m] === undefined || availableTroops[m] === undefined) {
    // ...
}

or:

if (null in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0} || undefined in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0}) {
Lucas Jones
You should use `typeof foo == "undefined"` as undefined can be set as a variable with a different value.
Sean Kinsey
what is typeof foo and why should I use it?
Fernando SBS
i´m going to use if (null in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0} || undefined in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0})should I use typeof foo ??
Fernando SBS
i´m getting error: missing : after property id
Fernando SBS
Ah. I was worried this trick might not work with the more complicated expressions. Give me a couple of minutes and I'll try to find a work-around.
Lucas Jones
I don't think there's a simple workaround - I'd use one of the simpler versions either in my answer or another. http://pastebin.com/8Q5NJCjL contains a possible one if you really want to use the second way I demonstrated, but it's more of an academic exercise than something that should actually be used.
Lucas Jones
Your object property example won’t work: `var x=null; x in {x:0}` yields *false*.
Gumbo
+1  A: 

This is the best way to do what you want

if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m]) {
    ...
}

The ! operator coerces the result into a boolean that can be tested for (null and undefined becomes false), and with the ! in front false is negated into true.

The other way to do it is to test each expression against null and undefined.

function isNullOrUndefined(val) {
    return (val === null || typeof val == "undefined"); 
}    

if (isNullOrUndefined(a) || isNullOrUndefined(b) ... ) {

And so you know it, the correct way to test for undefined is

if (typeof foo === "undefined") {...
Sean Kinsey
I think you mean `==`, not `=`.
John Feminella
Be careful with the non-specific test. A non-null boolean value of **false** will be treated the same as **null** unless you test for undefined/null.
John Fisher
The only problem with this is that `availableTroops[m]` might legitimately be 0, which might need a different behavior than null or undefined.
Gabe
@John: No, === is the way to go!!
Claudiu
@John sure did :)
Sean Kinsey
@Claudio: it is guaranteed that `typeof` returns a string, so `===` is not needed.
Sean Kinsey
@Claudio: please don't abuse your privilege to edit.
Sean Kinsey
if I use if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m]) { ...}I believe I can have problems if the value of one of the variables is 0. Am I wrong?
Fernando SBS
No, you are absolutely right; so in this case you should go for a function like `isSet` or `isNullOrUndefined` mentioned in the answers.But in your case, will `0` be treated different than not being set? Often 0 and not set can be treated as the same.
Sean Kinsey
A: 
if (tipoTropaPrioritaria[m] && troopsCount[m] && availableTroops[m]) { }
else { /* At least ones is undefined/null OR FALSE */ }

if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null)
{ /* One is null. */ }
John Fisher
What happens if one of them is `0` in the first code snippet?
Matt
@Matt That's what the "OR FALSE" comment was meant to address (zero, false, null)
John Fisher
@John: Good point :P. Apologies
Matt
+1  A: 

The way to do is:

if ((tipoTropaPrioritaria[m] == null || tipoTropaPrioritaria[m] == undefined)
|| (troopsCount[m] == null || troopsCount[m] == undefined) ||
(availableTroops[m] == null || availableTroops[m] == undefined)) {
...
}
Web Logic
+3  A: 

You could define a small helper function that does the check and then use it on all the values:

function notset(v) {
   return (v === undefined) || (v === null);
}

if (notset(tipoTropaPrioritaria[m]) || notset(troopsCount[m]) ||
    notset(availableTroops[m])) {
  ...
}
sth
good, but a little complicated =)
Fernando SBS
Just out of curiousity, would either of these work? http://pastebin.com/eA3L18qZ
Lucas Jones
@Lucas: I guess those should work, but probably it would be more readable to use a named function instead of defining it "inline".
sth
+1  A: 

If you do this a lot you can create a helper function

function isNullOrUndef(args) {
    for (var i =0; i < arguments.length; i++) {
        if (arguments[i] == null || typeof arguments[i] === "undefined") {
            return true
        }
    }
    return false
}

if (isNullOrUndef(tipoTropaPrioritaria[m], troopsCount[m], availableTroops[m]))
  ...
Alex K.
A: 

if you want to check if they are null or undefined you can write

if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m])

both null and undefined are falsely values. Then this will only be true if none of this are null or undefined. Please consider that there are other falsely values as well:

  • 0
  • empty string
  • NaN
Dani Cricco