Re:JS1k demo: “Highway at Night”
by jhgg on Sunday November 23, 2014 @03:36PM
(#8649848)
It's a bit more than that. It can also be used to convert strings (containing numeric values) to Numbers. More commonly seen than the prefix "~~" is "|0", because of it's low order precedence.
[ Parent
| on HN ]
Re:JS1k demo: “Highway at Night”
by owenversteeg on Sunday November 23, 2014 @08:52PM
(#8650679)
Everyone I know uses the + operator, I rarely see others. Funny that I'm not seeing + here.
With ~~foo you will always get an integer that fits inside a 32-bit integer range... regardless of the input... invalid numbers will be 0. Whereas with a +foo you may get NaN (for an invalid decimal input).
Part of this is because bitwise operations will convert whatever the input value is to a 32-bit integer value before performing the operation, and not fail (invalid input becomes 0 opposed to NaN).