Restrict Number to Min / Max Range

suggest change

If you need to clamp a number to keep it inside a specific range boundary

function clamp(min, max, val) {
    return Math.min(Math.max(min, +val), max);
}

console.log(clamp(-10, 10, "4.30")); // 4.3
console.log(clamp(-10, 10, -8));     // -8
console.log(clamp(-10, 10, 12));     // 10
console.log(clamp(-10, 10, -15));    // -10

Use-case example (jsFiddle)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arithmetic Math:
* Restrict Number to Min / Max Range

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
18 Arithmetic Math
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde