r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

3

u/wthit56 Mar 19 '19

(((n % 360) + 360) % 360)

Wraps all values to within 0 - 360 (pattern can be applied to any range).

1

u/420-jesus Mar 19 '19
(((n % 360) + 360) % 360)
=== (n % 360 % 360 + 360 % 360) % 360
=== (n % 360 % 360 + 0) % 360
=== n % 360 % 360 % 360
=== n % 360 

2

u/wthit56 Mar 19 '19

I don’t think that work for negative numbers, and negative numbers lower than -360.

→ More replies (1)