3

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

8
  • Refer to MDN Operators. It's easier than Googling the actual operator if you don't know the name of it. Commented Jul 11, 2013 at 18:29
  • 2
    There's always, what it is called again...oh, right! The specification. ;-) 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...) Commented Jul 11, 2013 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. Commented Jul 11, 2013 at 18:35
  • @Bergi yeap duplicate thanks but I cant found either google or stackoverflow when I searched for it... Commented Jul 11, 2013 at 18:36
  • 1
    @nahum: While the question title may be confusing (they often are), this answer explains it pretty well. Commented Jul 11, 2013 at 18:37

1 Answer 1

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.

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

3 Comments

~undefined, ~null, ~NaN, and ~{} all produce -1 in Chrome 28 console.
thanks man you always helping the community...
@NoahFreitas: What was I thinking? And here I tested several, but they were the non-NaN ones. sigh

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.