If you read the comments at the jQuery inArray
page here, there's an interesting declaration:
!!~jQuery.inArray(elm, arr)
Now, I believe a double-exclamation point will convert the result to type boolean
, with the value of true
. What I don't understand is what is the use of the tilde (~
) operator in all of this?
var arr = ["one", "two", "three"];
if (jQuery.inArray("one", arr) > -1) { alert("Found"); }
Refactoring the if
statement:
if (!!~jQuery.inArray("one", arr)) { alert("Found"); }
Breakdown:
jQuery.inArray("one", arr) // 0
~jQuery.inArray("one", arr) // -1 (why?)
!~jQuery.inArray("one", arr) // false
!!~jQuery.inArray("one", arr) // true
I also noticed that if I put the tilde in front, the result is -2
.
~!!~jQuery.inArray("one", arr) // -2
I don't understand the purpose of the tilde here. Can someone please explain it or point me towards a resource?
There's a specfic reason you'll sometimes see
~
applied in front of$.inArray
.Basically,
is a shorter way to do
$.inArray
returns the index of the item in the array if the first argument is found, and it returns -1 if its not found. This means that if you're looking for a boolean of "is this value in the array?", you can't do a boolean comparison, since -1 is a truthy value, and when $.inArray returns 0 (a falsy value), it means its actually found in the first element of the array.Applying the
~
bitwise operator causes-1
to become0
, and causes 0 to become `-1. Thus, not finding the value in the array and applying the bitwise NOT results in a falsy value (0), and all other values will return non-0 numbers, and will represent a truthy result.And it'll work as intended.
jQuery.inArray()
returns-1
for "not found", whose complement (~
) is0
. Thus,~jQuery.inArray()
returns a falsy value (0
) for "not found", and a truthy value (a negative integer) for "found".!!
will then formalise the falsy/truthy into real booleanfalse
/true
. So,!!~jQuery.inArray()
will givetrue
for "found" andfalse
for "not found".The
~
for all 4 bytesint
is equal to this formula-(N+1)
SO
I assume, since it is a bitwise operation, it is the fastest (computationally cheap) way to check whether path appears in modifiedPaths.
You're right: This code will return
false
when theindexOf
call returns -1; otherwisetrue
.As you say, it would be much more sensible to use something like
The tilde operator isn't actually part of jQuery at all - it's a bitwise NOT operator in JavaScript itself.
See The Great Mystery of the Tilde(~).
You are getting strange numbers in your experiments because you are performing a bitwise logical operation on an integer (which, for all I know, may be stored as two's complement or something like that...)
Two's complement explains how to represent a number in binary. I think I was right.