简体   繁体   中英

Using multiple conditions within an if statement

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.

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 a == 1 || a == 2 || a ==3 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 1 || 2 || 3 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 0 || False || 5 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 compatible 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM