r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

40

u/a_dev_has_no_name Mar 19 '19 edited Mar 20 '19

~~anyNumber

Very short Bitwise operation to do rounding similar to the equivalent of Math.floor() i.e ~~5.9 // 5

It's handy for code golf or for angering coworkers

Edit: I didn't quite literally mean it's exactly the same as Math.floor I should've clarified on that. Also, I'm not suggesting anyone use this in production code, like I said it's nice for code golf.

28

u/anakappa Mar 19 '19

This is why we can't have nice things.

10

u/a_dev_has_no_name Mar 19 '19

Wait until you see the other tricks ಠ‿ಠ