DEV Community

Discussion on: Getting the integer part from a number in Javascript

Collapse
 
costinmanda profile image
Costin Manda

Thanks! To my shame I've never actually used either Math.trunc or ** in Javascript :D Live and learn. I'll update the post.

Collapse
 
fetishlace profile image
fetishlace

Various people prefer various tools, it is not the shame i guess :-)
** is newer and handy, way better 2**16 than write Math.pow(2,16)
And I would rather use Math.trunc() than Number.parseInt() - which is in play here too. In fact parseInt(inputValue, 10) with explicit radix argument is the right way to go according to Airbnb standards.
That double bit flip ~~ looks nice and short, and it could run faster than others - that is why it has it's place - but some people are really confused seeing it + this is not a bottleneck normally anyway + it "breaks" sooner than those alternatives for higher numbers...
Anyway I am seeing all of these in other people's code.