0

Other than a slight performance gain, is there an advantage to using !~x as true vs. checking to see if ~-1 ?

2 Answers2

3

There can be a difference for large numbers. Because you're confined to a 32-bit range, consider the following.

var x = Math.pow(2,32)-1; // 4294967295

console.log(~x); // 0

So here you get a 0 result giving you the opposite boolean conversion than you'd expect.

  • 2
    Still not sure if this answers the question, because I'm not sure what the question actually is. :( – Shmiddty May 31 '13 at 20:52
  • @Shmiddty: I'm pretty sure your comment under the question describes what the asker is getting at. But if this is an Array or Array-like collection that can have very high indices, there can be an unexpected results. –  May 31 '13 at 20:56
  • 1
    I definitely +1'd this because it's a good thing to note. – Shmiddty May 31 '13 at 21:08
0

There's no behavioral difference. Use whichever is clearest for the scenario you're implementing.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63