Skip to main content

Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
a day ago (1.12.0)
function clamp
clamp(
value: number,
maximum: number,
): number

Clamps a number within the inclusive lower and upper bounds.

This function takes a number and two bounds, and returns the number clamped within the specified bounds. If only one bound is provided, it returns the minimum of the value and the bound.

Examples

Example 1

const result1 = clamp(10, 5); // result1 will be 5, as 10 is clamped to the bound 5 const result2 = clamp(10, 5, 15); // result2 will be 10, as it is within the bounds 5 and 15 const result3 = clamp(2, 5, 15); // result3 will be 5, as 2 is clamped to the lower bound 5 const result4 = clamp(20, 5, 15); // result4 will be 15, as 20 is clamped to the upper bound 15

Parameters

value: number
  • The number to clamp.
maximum: number
  • The maximum bound to clamp the number.

Return Type

The clamped number within the specified bounds.

clamp(
value: number,
minimum: number,
maximum: number,
): number

Parameters

value: number
minimum: number
maximum: number

Return Type

Add Package

deno add @es-toolkit/es-toolkit

Import symbol

import { clamp } from "@es-toolkit/es-toolkit";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { clamp } from "@es-toolkit/es-toolkit";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { clamp } from "@es-toolkit/es-toolkit";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { clamp } from "@es-toolkit/es-toolkit";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { clamp } from "@es-toolkit/es-toolkit";