2

Hi im deleting an array from specific index, and I came with this script:

var arr = [1,2,3,4];
var index = 2;
if (~index) arr.splice(index, 1);

I google "what does "~" operator do in this script?" and I can't find any answer I guess I'm doing an wrong search can anyone could explain me this operator and what's he name for a properly search?

I have an little suspect that is kind of comparing against (!)(undefined) but not sure...

thanks in advance

ncubica
  • 7,276
  • 7
  • 50
  • 67
  • Refer to [MDN Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators). It's easier than Googling the actual operator if you don't know the name of it. –  Jul 11 '13 at 18:29
  • 2
    There's always, what it is called again...oh, right! [The specification](http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.8). ;-) Literally the first thing that comes up if you open the spec and search in it for `~`. (Not that trying to search for it in a search engine wasn't a good try, I'm just linking to the spec with a bit of well-intentioned humor...) – T.J. Crowder Jul 11 '13 at 18:30
  • @scrappedcola About the duplicated question, while the questions have similarities NOTHING referred javascript in the question. Since this Bitwise Operator (now I know how call it) is from javascript (EcmaScript) no from jQuery so I think is not duplicate it. – ncubica Jul 11 '13 at 18:35
  • @Bergi yeap duplicate thanks but I cant found either google or stackoverflow when I searched for it... – ncubica Jul 11 '13 at 18:36
  • And I'm asking what does in this script as T.J. Crowder explained to me.. thanks – ncubica Jul 11 '13 at 18:37
  • 1
    @nahum: While the question title may be confusing (they often are), [this answer](http://stackoverflow.com/a/9316724/) explains it pretty well. – Bergi Jul 11 '13 at 18:37
  • @nahum: Try http://symbolhound.com/ if you want to search a character (or operator) whose name you don't know – Bergi Jul 11 '13 at 18:39
  • nahum: The `~` behaves the same irrespective of what expression it precedes. Doesn't matter if it's operating on the value from a variable, or returned from a jQuery method. It's the same. –  Jul 11 '13 at 18:46

1 Answers1

4

what does “~” operator do in this script?

As others have pointed out, it's the bitwise NOT operator. Which is all well and good, but what's it doing in this script was the question. :-)

The idea was probably to do pretty much what you said: If index is a number, do the splice. The first thing the ~ operator does to its operand is convert it to a number if it can. If it can't, the result is NaN ("not a number"), which is falsey, and so the condition would be false and the splice wouldn't happen.

But the conversion doesn't result in NaN nearly as often as I suspect the author of that code thought. :-)

Some random examples of things that won't do the splice:

~-1 === 0

And some that will do the splice:

~"foo" === -1
~0 === -1
~1 === -2
~2 === -3
~true === -2
~false === -1
~-2 === 1
~undefined === -1
~null === -1
~NaN === -1

Probably not ideal that it's trying to do the splice with some of those. For instance, the true will make it do a splice using index 1, the false will be index 0.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639