Open In App
Related Articles

Lodash _.clamp() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash _.clamp() method is used to clamp numbers within the inclusive range within the lower and upper bounds.

Syntax:

_.clamp(number, lower, upper);

Parameters:

  • number: This parameter holds the number to clamp.
  • lower: This parameter holds the lower bound.
  • upper: This parameter holds the upper bound.

Return Value:

This method returns the clamped number.

Example 1: In this example, we are printing the clamped number by the use of the _.clamp() method.

Javascript

// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.clamp method
console.log(_.clamp(2, 3, 5));
console.log(_.clamp(2, -5, -2));

                    

Output:

3
-2

Example 2: In this example, we are printing the clamped number by the use of the _.clamp() method.

Javascript

// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.clamp method
console.log(_.clamp(15, -13, 13));
console.log(_.clamp(-15, -13, 13));

                    

Output:

13
-13

Last Updated : 20 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments