2

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.

8
  • (a == (1 || 2 || 3)) is invalid syntax Commented Apr 8, 2019 at 17:55
  • 1
    @KevinVandy How is that? No syntax errors in the code. Commented Apr 8, 2019 at 17:57
  • @KevinVandy It's not at all (see my answer)! Commented Apr 8, 2019 at 17:59
  • @Teemu Ok, correction, it is misleading syntax Commented Apr 8, 2019 at 17:59
  • 1
    @KevinVandy it is a logical error, not a syntax error. Commented Apr 8, 2019 at 18:01

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

One of the best one-liners.
6

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));

3 Comments

You can have || like that, it just does not do what the OP assumed it did. I think you should clarify what it WAS doing.
@Marie See my answer :)
@Marie I guess Pranav explains it perfectly... 😊
4

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.

Comments

0

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));

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.