How can I use JavaScript to limit a number between a min/max value?

前端 未结 7 1114
醉梦人生
醉梦人生 2020-12-04 16:15

I want to limit a number between two values, I know that in PHP you can do this:

$number = min(max(intval($number), 1), 20);
// this will make $number 1 if i         


        
相关标签:
7条回答
  • 2020-12-04 17:04

    Use lodash's clamp method:

    _.clamp(22, 1, 20) // Outputs 20

    0 讨论(0)
提交回复
热议问题