r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

5

u/HideousNomo Mar 19 '19

I've been using num | 0 for this very thing

1

u/fgutz Mar 19 '19

so I understand what's going with this with integers, 23|0 will always return 23 because you're comparing 10111 with 00000 so it will always return 10111.

So I assume this just casts the float to an integer and does the comparison, effectively doing a pseudo math.floor.

Like /u/rcfox pointed out, it doesn't work for negative integers:

Math.floor(-21.12) === -22
// but 
-21.12|0 === -21