0

I'm reading this javascript function:

if (~['bacon', 'burger'].indexOf(type)) {
    this.res.writeHead(200, { 'Content-Type': 'text/plain' });
    this.res.end('Serving ' + type + ' sandwich!\n');
  }

But I'm not sure what means ~ some one know when I use it or what meaning?

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • 3
    [JS bit-wise operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) – ajp15243 Apr 30 '14 at 13:56
  • 1
    Bitwise NOT ~ a Inverts the bits of its operand. – Benjamin RD Apr 30 '14 at 13:57
  • Bitwise NOT operator -> [MDN reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise_operators) – Jonathan Apr 30 '14 at 13:57
  • possible duplicate of [What does a tilde do when it precedes an expression?](http://stackoverflow.com/questions/12299665/what-does-a-tilde-do-when-it-precedes-an-expression) – Cerbrus Apr 30 '14 at 13:59

4 Answers4

9

~ is the bitwise NOT operator. It toggles every bit of a number.

  • 0 becomes -1.
  • -1 becomes 0.
  • No other numbers become zero.

That means that

if (~['bacon', 'burger'].indexOf(type)) {

is a confusing way of writing

if (['bacon', 'burger'].indexOf(type) == -1) {

indexOf returns -1 when it doesn't find the string.

ikegami
  • 367,544
  • 15
  • 269
  • 518
3
~ is a Bitwise NOT operator...

Read more

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
2

In this instance, the ~ allows that code to turn the return value of .indexOf() — which is a number indicating the position of the searched-for value in the array — into a boolean. In other words, it takes the "where is the value" result and turns it into a "is the value in the list" result.

How? Well, .indexOf() returns -1 when the value is not found, and a number greater than or equal to zero if it is. The ~ operator converts its numeric argument to a 32-bit integer and then inverts every bit. That process happens to turn -1 to 0, and any positive integer to some negative non-zero value, and 0 to -1. When such results are subsequently examined as boolean values, the original -1 will be false (because 0 is "falsy") while the integers greater than or equal to zero will be true (because they're all converted to some non-zero value).

Pointy
  • 405,095
  • 59
  • 585
  • 614
-1

Bitwise NOT (~ a) Inverts the bits of its operand.

EXAMPLE

  • 9 (base 10) = 00000000000000000000000000001001 (base 2)

           --------------------------------
    
  • ~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • Don't use someone's comment on the question as an answer. And don't use examples from **[other sites](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators)** without giving credit – Cerbrus Apr 30 '14 at 13:59
  • Please don't down-vote my answer for no reason at all in the future – Jebathon Apr 30 '14 at 14:04
  • At the time of my comment / vote, you only had some sample code copied from the dev.mozilla site in your answer. Even now, all you have is that example and a link, without an explanation. _"Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect."_ **[Help center link](http://stackoverflow.com/help/privileges/vote-down)** – Cerbrus Apr 30 '14 at 14:07