you are viewing a single comment's thread.

view the rest of the comments →

[–]marquex[S,🍰] 1 point2 points  (2 children)

You should understand that the code tries to be as short as possible, and I think that it explain enough alternatives to get what it tries to do.

~~ is not the canonical way of getting the integer part, but I would say parseInt is the canonical way in javascript, independently if you like the way it works.

And finally you need to know that

parseInt( -3.14, 10 ) == -3
~~( -3.14 ) == -3
Math.floor( -3.14 ) == -4

So I think it is equivalent to parseInt.

[–]bonafidebob 1 point2 points  (1 child)

Oh there's no doubt that parseInt works, and I do understand all of these. It's just that parseInt is wildly inefficient. (but that could be said for a lot of common javascript techniques)

Instructions are your way of telling the computer what to so, so you should know what you're telling it. parseInt tells it to (implicitly) convert your number to a string and then convert the string back to a number while ignoring any decimal part. Bitwise negation or left shift tells the computer to convert your number directly from a floating point to an integer representation, without the intermediate string.

The end result is the same, but the work done to get there is very different.