"Limiting the Range of a Number": Snowman

Summary

This recipe demonstrates how to limit a numeric variable to a value between a set range, this process is commonly known as clamping.

The example adds a clamp() function to the existing built-in Math global JavaScript object, which can then be called to achieve the desired result. It also adds a clamp() function to the built-in Number global JavaScript object that the new Math.clamp() function uses internally.