admin管理员组

文章数量:1426347

How using multiple conditions within an if statement?

function testNum(a) {
  if (a == (1 || 2 || 3)) {
    return "is 1, 2, or 3";
  } else {
    return "is not 1, 2, or 3";
  }
}

console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3)); // returns "is not 1, 2, or 3"

How using multiple conditions within an if statement?

function testNum(a) {
  if (a == (1 || 2 || 3)) {
    return "is 1, 2, or 3";
  } else {
    return "is not 1, 2, or 3";
  }
}

console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3)); // returns "is not 1, 2, or 3"

testNum(2) and testNum(3) should return: "is 1, 2 or 3" but doesn't.

Share Improve this question edited Apr 8, 2019 at 17:58 double-beep 5,53719 gold badges40 silver badges49 bronze badges asked Apr 8, 2019 at 17:53 Sky Red TeaSky Red Tea 291 bronze badge 8
  • (a == (1 || 2 || 3)) is invalid syntax – Kevin Vandy Commented Apr 8, 2019 at 17:55
  • 1 @KevinVandy How is that? No syntax errors in the code. – Teemu Commented Apr 8, 2019 at 17:57
  • @KevinVandy It's not at all (see my answer)! – Joe Iddon Commented Apr 8, 2019 at 17:59
  • @Teemu Ok, correction, it is misleading syntax – Kevin Vandy Commented Apr 8, 2019 at 17:59
  • 1 @KevinVandy it is a logical error, not a syntax error. – Isaac Vidrine Commented Apr 8, 2019 at 18:01
 |  Show 3 more ments

6 Answers 6

Reset to default 8

In this particular scenario, you can even use an array and Array#includes method for checking.

if ([1, 2, 3].includes(a)) {
  // your code
}

function testNum(a) {
  if ([1, 2, 3].includes(a)) {
    return "is 1, 2, or 3";
  } else {
    return "is not 1, 2, or 3";
  }
}
console.log(testNum(1));
console.log(testNum(2));
console.log(testNum(4));
console.log(testNum(3));

FYI : In your current code (1 || 2 || 3) results 1(since 1 is truthy) and actually a == (1 || 2 || 3) does a == 1. The right way is to seperate each conditions with || (or), for eg : a == 1 || a == 2 || a ==3.

For more details visit MDN documentation of Logical operators.

You cannot have || like that. The one you have used is not the right way. You should be using:

function testNum(a) {
  if (a == 1 || a == 2 || a == 3) {
    return "is 1, 2, or 3";
  } else {
    return "is not 1, 2, or 3";
  }
}

console.log(testNum(1));
console.log(testNum(2));
console.log(testNum(3));

Your or-operators are placed incorrectly:

function testNum(a) {
    if (a == 1 || a == 2 || a == 3) {
        return "is 1, 2, or 3";
    } else {
        return "is not 1, 2, or 3";
    }
}

Before, you were testing if a was equal to 1 || 2 || 3 which evaluates to 1 †. So you were just checking a == 1 which is not what you wanted!

† Essentially when you string together "or"s like this, the first truthy value is returned. For example, you can assert for yourself that: 0 || False || 5 gives 5.

You can try using a list of qualified values and check if the lookup value belongs there:

function testNum(a) {
    var candidates = [1, 2, 3];
    if (candidates.indexOf(a) != -1) {
        return "is 1, 2, or 3";
    } else {
        return "is not 1, 2, or 3";
    }
}

console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3));
console.log(testNum(4));

You can either

    if (a == 1 || a == 2 || a == 3) {

or

    if ([1, 2, 3].includes(a)) {

which will be more convenient when there's more values to test. If you want to be patible with ancient browsers, you have to write it like this

    if ([1, 2, 3].indexOf(a) >= 0) {

which look undeniably uglier.

The reason why you are not getting the expected result because you are performing a Logical OR operation here.

As per MDN Network : Logical OR (||) expr1 || expr2 If expr1 can be converted to true, returns expr1; else, returns expr2.

(1 || 2 || 3) is evaluated and the operation results at 1. You can check the result by pasting (1 || 2 || 3) in Chrome debugger or something.

So when the first number is 1 in all cases, the expression result is also 1. You can change the expression as how others have advised to get the desired output.

本文标签: javascriptUsing multiple conditions within an if statementStack Overflow