 # Double tilde?

I see some scripts put two tildes in front of variables, like so:

``````var a = ~~b;

``````

As far as I can tell, all this does is ensure that a is given a number value. But is there a reason to prefer ~~ over +?

``````var a = +b;

``````

Or is there something else going on?

Never knew that one.
Looked it up, and it does -(N+1)

So, if you have 1 ~~"2", you get the following:

``````-(-(2+1)+1) =
-(-(3)+1) =
-(-2) =
2

``````

Read [this article. It points out that is handy to handle [url=http://en.wikipedia.org/wiki/Sentinel_value]sentinel values](http://dreaminginjavascript.wordpress.com/2008/07/04/28/).

~ actually does a 2s complement on the individual bits. In any language except JavaScript doing bit operations like that would be a billion times faster than doing arithmetic operations. JavaScript is extremely slow at bit operations so any other way would probably be faster than using bit operators (which is why you don't often see them used in JavaScript). Generally only programming gurus who are also JavaScript newbies would consider them.

Wow, bookmarked.