DEV Community

Cover image for Golfed Min/Max
Zevan Rosser
Zevan Rosser

Posted on

4

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay