19

I think I'm missing something basic here. Why is the third IF condition true? Shouldn't the condition evaluate to false? I want to do something where the id is not 1, 2 or 3.

var id = 1;
if(id == 1) //true    
if(id != 1) //false 
if(id != 1 || id != 2 || id != 3) //this returns true. why?

Thank you.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
tempid
  • 7,838
  • 28
  • 71
  • 101
  • 6
    because `id` does not equal `2` or `3`? `false || true || true == true` – J. Holmes Feb 03 '12 at 17:11
  • 3
    1 != 2 .. this is obviously true.. lollzzz – dku.rajkumar Feb 03 '12 at 17:12
  • Then it should return `false`, I think? Sorry, I've been working all night so not able to think clearly. – tempid Feb 03 '12 at 17:13
  • [Or](http://en.wikipedia.org/wiki/Logical_disjunction) vs [And](http://en.wikipedia.org/wiki/Logical_conjunction) – J. Holmes Feb 03 '12 at 17:14
  • @dku.rajkumar the `var i = 1` was just an example. I need to do work on some 50 ids but skip where id is 1, 2, or 3. – tempid Feb 03 '12 at 17:16
  • @user349308: If *any* of the conditions are `true`, it will return `true`. Remeber that `!=` means *does NOT equal*, so `1 != 2` is a `true` statement, so it returns `true`. –  Feb 03 '12 at 17:16
  • @user349308: *"skip where id is 1, 2, or 3."*, Then do `if(!(id==1 || id==2 || id==3))` or `if(id!=1 && id!=2 && id!=3)` –  Feb 03 '12 at 17:18

6 Answers6

40

With an OR (||) operation, if any one of the conditions are true, the result is true.

I think you want an AND (&&) operation here.

user66001
  • 774
  • 1
  • 13
  • 36
Gabe
  • 1,166
  • 1
  • 12
  • 15
27

You want to execute code where the id is not (1 or 2 or 3), but the OR operator does not distribute over id. The only way to say what you want is to say

the id is not 1, and the id is not 2, and the id is not 3.

which translates to

if (id !== 1 && id !== 2 && id !== 3)

or alternatively for something more pythonesque:

if (!(id in [,1,2,3]))
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    The alternative checks for the index. Not if the value is in the array. Unlike in python where it checks for the value. `array.indexOf(id) == -1` means that the id isn't in the array. use `!=` for the opposite. – Neil Aug 05 '17 at 22:29
  • 1
    @NeilDesh, note the `,` at the start. They're equivalent – Mike Samuel Aug 05 '17 at 22:32
  • @MikeSamuel Then why is `4 in [,4,2,3]` false – Neil Aug 07 '17 at 18:19
  • 1
    @NeilDesh, the trick is to have a partial identity mapping. It only works for small counting numbers so it's not generally useful. To represent the set (4) try `[,,,,4]`. – Mike Samuel Aug 07 '17 at 21:18
  • This answer is misleading as it checks the index not the value of the array. The correct way is to use `Array.includes(value)`. – denisb411 Nov 07 '22 at 18:21
  • 1
    @denisb411 The partial identity array maintains an equivalence between key and value. You're right about `.includes`, but there was no such thing when I wrote that in 2012. Happy to accept an edit or up-vote a new answer. – Mike Samuel Nov 08 '22 at 01:05
4

Each of the three conditions is evaluated independently[1]:

id != 1 // false
id != 2 // true
id != 3 // true

Then it evaluates false || true || true, which is true (a || b is true if either a or b is true). I think you want

id != 1 && id != 2 && id != 3

which is only true if the ID is not 1 AND it's not 2 AND it's not 3.

[1]: This is not strictly true, look up short-circuit evaluation. In reality, only the first two clauses are evaluated because that is all that is necessary to determine the truth value of the expression.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
2

When it checks id!=2 it returns true and stops further checking

Dhiraj Valechha
  • 161
  • 1
  • 2
  • 9
  • 2
    This is important to note too; it comes in handy if you need to see if an object exists before checking a parameter. `if(obj != null && obj.field == 2)` – Jeffrey Sweeney Feb 03 '12 at 17:18
1

because the OR operator will return true if any one of the conditions is true, and in your code there are two conditions that are true.

Seth
  • 558
  • 3
  • 6
0

This is an example:

false && true || true   // returns true
false && (true || true) // returns false
(true || true || true)  // returns true
false || true           // returns true
true || false           // returns true
JJJ
  • 32,902
  • 20
  • 89
  • 102
haythem
  • 21
  • 1