DEV Community

Cover image for Golfed Min/Max
Zevan Rosser
Zevan Rosser

Posted on

Golfed Min/Max

Math.min(a,b)  // 13 chars
a<b?a:b        //  7 chars

Math.max(a,b)
a>b?a:b
Enter fullscreen mode Exit fullscreen mode

Another small golfing gem from codegolf stackexchange. This isn’t immediately obvious, but cool to note when golfing.

It’s also worth mentioning that if your code is long enough, aliasing Math.min and/or Math.max may be shorter in the long run:

m = Math.min
Math.min(a,b)  // 13 chars
a<b?a:b        //  7 chars
m(a,b)         //  6 chars
Enter fullscreen mode Exit fullscreen mode

See more stuff like this over @ Snippet Zone

Top comments (0)