Uint8ClampedArray
Creates a new Uint8ClampedArray typed array where values are clamped between 0 and 255 instead of wrapping
Syntax
new Uint8ClampedArray(length) or new Uint8ClampedArray(source)Parameters
| Parameter | Type | Description |
|---|---|---|
| length | number | ArrayBuffer | ArrayLike<number> | Array length, buffer, or array-like source |
Return Value
A new Uint8ClampedArray instance
Examples
const arr = new Uint8ClampedArray(3)
arr[0] = 300 // clamped to 255
arr[1] = -10 // clamped to 0
console.log(arr) // Uint8ClampedArray [255, 0, 0]const pixels = new Uint8ClampedArray([255, 0, 0, 255]) // RGBA red pixel
console.log(pixels)function adjustBrightness(data: Uint8ClampedArray, factor: number) {
for (let i = 0; i < data.length; i += 4) {
data[i] = data[i] * factor // R (auto-clamped)
data[i+1] = data[i+1] * factor // G
data[i+2] = data[i+2] * factor // B
}
}Understanding Uint8ClampedArray
The Uint8ClampedArray method in JavaScript creates a new Uint8ClampedArray typed array where values are clamped between 0 and 255 instead of wrapping. It belongs to the TypedArray object and is one of the most widely used methods for working with typedarray values in modern JavaScript and TypeScript applications.
The method signature is new Uint8ClampedArray(length) or new Uint8ClampedArray(source). It accepts 1 parameter: length. When called, it returns a new uint8clampedarray instance. Understanding when and how to use Uint8ClampedArray() helps you write more expressive, readable code.
Common use cases for Uint8ClampedArray include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like uint8array-constructor, int8array-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Used extensively with Canvas ImageData. Supported in all modern browsers and Node.js.
Browser Compatibility
Used extensively with Canvas ImageData. Supported in all modern browsers and Node.js.
Related Methods
More TypedArray Methods
Other methods in the TypedArray object
Related Tools
More TypedArray Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.