Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ~~ in JavaScript? [duplicate]

I was just messing with random stuff, while I found something interesting..

if I have ~ before a number, for example I have tried

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

is it making it negative and reducing it by 1? I don't have any idea, can anyone pleas explain this??

like image 413
Adarsh Hegde Avatar asked Sep 16 '15 11:09

Adarsh Hegde


People also ask

What does ~~ mean in JS?

The Complete Full-Stack JavaScript Course! The “double tilde” (~~) operator is a double NOT Bitwise operator. Use it as a substitute for Math. floor(), since it's faster.

What is difference == and === in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What does double tilde mean in JS?

That ~~ is a double NOT bitwise operator. It is used as a faster substitute for Math. floor() for positive numbers.


1 Answers

The operator ~ returns that result:

~N = -(N+1)

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

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

~~'-1' = -1
~~true = 1
~~false = 0
~~5.6 = 5
like image 185
Joanvo Avatar answered Oct 18 '22 22:10

Joanvo