What is ~~ in JavaScript?

I was just messing around with random things while I found something interesting.

if I have ~ in front of the number, for example, I tried

~110100100 // result will be  " -110100101 "
~11 // result will be " -12 "

does it make it negative and reduces it by 1? I have no idea if anyone can explain this?

+4
source share
1 answer

The operator ~returns this result:

~N = -(N+1)

But this is the effect of inverting the value of all bits of the variable.

Double tide is ~~used to convert some types to int, since the operator ~converts the value to a 32-bit int before inverting its bits. Thus:

~~'-1' = -1
~~true = 1
~~false = 0
~~5.6 = 5
+5
source

Source: https://habr.com/ru/post/1607638/


All Articles