DEV Community

Discussion on: Number Truncation in JavaScript

Collapse
 
moopet profile image
Ben Sinclair

I didn't know about trunc and I would have just used parseInt without giving it a thought, but that gotcha you explain is a good point.

Unless I was mimimising/golfing or there's a tiny performance benefit and you're performing the operation a gazillion times per second, I'd stay clear of the bitwise tricks. They're curiosities but they make the code difficult to read.

Collapse
 
samanthaming profile image
Samantha Ming

I guess bitwise operators are not as popular, so if you use it in your code it’s probably going to make a few people spin their heads as to what’s going on 😵... honestly, I have never used bitwise operator in my code before 😥 do you know if there are any other gotcha with using bitwise operators?

Collapse
 
moopet profile image
Ben Sinclair

I don't think so, but I like to write code as if the person maintaining it isn't familiar with all these little tricks!

Thread Thread
 
samanthaming profile image
Samantha Ming

Totally! Writing code is like communication, sure you can use all the fancy lingo. But if no one understands you, are you actually communicating. Good call! 👍