What does a tilde do when it precedes an expression?
~
is a bitwise operator that flips all bits in its operand.
For example, if your number was 1
, its binary representation of the IEEE 754 float (how JavaScript treats numbers) would be...
0011 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
So ~
converts its operand to a 32 bit integer (bitwise operators in JavaScript do that)...
0000 0000 0000 0000 0000 0000 0000 0001
If it were a negative number, it'd be stored in 2's complement: invert all bits and add 1.
...and then flips all its bits...
1111 1111 1111 1111 1111 1111 1111 1110
So what is the use of it, then? When might one ever use it?
It has a quite a few uses. If you're writing low level stuff, it's handy. If you profiled your application and found a bottleneck, it could be made more performant by using bitwise tricks (as one possible tool in a much bigger bag).
It's also a (generally) unclear trick to turn indexOf()
's found return value into truthy (while making not found as falsy) and people often use it for its side effect of truncating numbers to 32 bits (and dropping its decimal place by doubling it, effectively the same as Math.floor()
for positive numbers).
I say unclear because it's not immediately obvious what it is being used for. Generally, you want your code to communicate clearly to other people reading it. While using ~
may look cool, it's generally too clever for its own good. :)
It's also less relevant now that JavaScript has Array.prototype.includes()
and String.prototype.includes()
. These return a boolean value. If your target platform(s) support it, you should prefer this for testing for the existence of a value in a string or array.
What does -~ do in JavaScript?
This is (kind of?) clever use of the tilde (~
) operator, but it just leads to confusion. The ~
(effectively) adds one to the number and flips the sign.
~0 === -1
~1 === -2
~-1 === 0
etc.
The -
flips the sign back to what it originally was.
So the end result of -~j
is j + 1
This then gets added to a[i]
and assigned to y
Moral of the story: don't ever write code like this.
Note: There are legitimate use-cases for the ~
operator, most notably in the .indexOf()
function. If you want to check if something was found in an array/string, rather than saying:
if (arr.indexOf("foo") > -1) {...}
, you can say
if (~arr.indexOf("foo")){...}
. This is because if the value is not found, indexOf()
will return -1, which, when passed through the tilde operator, will return 0, which coerces to false. All other values return 0 through n, which return -(1 through n+1) when passed through the tilde operator, which coerce to true.
What does '!!~' do in Javascript?
indexOf
will return the index 0-x
if the element is found, -1
otherwise.
~
will change all the bits in the number, turning -1
to 0
(the binary represantation of -1
is 1111 1111 ...
).
0
is a falsy value, all other numbers are truthy.
!!
will convert a falsy value to false
and a truthy value to true
. It wouldn't be needed here, because every
doesn't care whether it recieves a truthy value or true.
As others have mentioned, nowadays you could use includes
. However, includes
is newer to the JavaScript ecosystem than indexOf
, so this solution would work in IE, the solution with include
would not. You could still use includes
in IE either by providing a polyfill or by transpiling your code.
How to correctly evaluate variable within R tilde expression
A possible solution:
col_grob <- function(pal) {
txt <- substitute(pal_bar(pal))
g <- ggplotify::as.grob(as.expression(txt))
grid::grid.draw(g)
}
col_grob(pal = c("red", "blue"))
~foo(arguments); What does ~ mean in this code snippet in nodejs?
In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right
before an indexOf() to do a boolean check (truthy/falsy) on a string.
On its own, indexOf() returns the index number of a String object
passed in. So if -1 is returned it will be turned into 0 which is
falsy.
Source: https://wsvincent.com/javascript-tilde/
what's the !~ mean in javascript
tl;dr
indexOf
returns -1 when an element cannot be found in an array. Therefore, the if
statement is checking if name
could not be found in names
. !~-1 ==> true
Longer version:
The tilde (~
) operator (bitwise NOT) yields the inverted value (a.k.a. one’s complement) of a. [Source] For example, ~-1 === 0
. Note that 0 == false
and !0 === true
. indexOf
returns -1 when an element cannot be found in an array. Therefore, we can use !~-1 === true
to find out if indexOf
could not find name
in names
(i.e. returned -1).
My opinion:
As you can see, using these obfuscated or "clever" techniques without comments can really confuse the reader. If you do love these techniques, please document what your line(s) of code are doing for the sake of the readers!
How does !!~ (not not tilde/bang bang tilde) alter the result of a 'contains/included' Array method call?
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.
How ~(tilde)Infinity becomes -1
In IEEE 754 floating point, the Infinity
constant is represented by a value with all the fraction bits set to 0. When that's coerced to a 32-bit integer value in calculating the bitwise complement (the ~
unary operator), you get just zero, so the complement is all 1 bits, or -1.
Positive infinity is:
01111111111100000000000000000000000000000000000000000000000000000
(give or take a zero). The sign bit is 0, the exponent is all 1 bits, and the mantissa is all zeros.
Related Topics
Prevent Execution of Parent Event Handler
Primitive Value VS Reference Value
Working with Select Using Angularjs's Ng-Options
Convert String with Commas to Array
How to Call a JavaScript Function on Page Load
How to Find Prime Numbers Between 0 - 100
How to Send JSON Instead of a Query String with $.Ajax
Operator Precedence with JavaScript Ternary Operator
How to Gain Access to the Closure of a Function
Is JavaScript Supported in an Email Message
What Does a Tilde Do When It Precedes an Expression
How to Loop Through an Array Containing Objects and Access Their Properties
Jquery - Setting the Selected Value of a Select Control via Its Text Description
How to Get Unique Values in an Array
Best Way to Iterate Over an Array Without Blocking the Ui