admin管理员组

文章数量:1313084

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)

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)

Share Improve this question asked May 24, 2010 at 21:07 FernandoSBSFernandoSBS 6555 gold badges12 silver badges23 bronze badges 3
  • 7 Do you mean the values null and undefined or the string values "null" and "undefined"? – Gumbo Commented May 24, 2010 at 21:10
  • not string values, the "internal javascript" null/undefined value. – FernandoSBS Commented May 24, 2010 at 21:33
  • Remember to accept a solution when you are satisfied. – Sean Kinsey Commented May 25, 2010 at 14:22
Add a ment  | 

7 Answers 7

Reset to default 3

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])) {
  ...
}

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}) {

The way to do is:

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

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]))
  ...

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 bees 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") {...
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. */ }

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

本文标签: operatorsJavascript how to compare multiple variables valueStack Overflow