Uint8Array
Creates a new Uint8Array typed array representing an array of 8-bit unsigned integers initialized to zero
Syntax
new Uint8Array(length) or new Uint8Array(buffer, byteOffset?, length?)Parameters
| Parameter | Type | Description |
|---|---|---|
| length | number | ArrayBuffer | ArrayLike<number> | Array length, buffer, or array-like source |
Return Value
A new Uint8Array instance
Examples
const bytes = new Uint8Array(4)
console.log(bytes) // Uint8Array [0, 0, 0, 0]const bytes = new Uint8Array([72, 101, 108, 108, 111])
const text = new TextDecoder().decode(bytes)
console.log(text) // 'Hello'const buffer = new ArrayBuffer(8)
const view = new Uint8Array(buffer, 0, 4)
view[0] = 255
console.log(view) // Uint8Array [255, 0, 0, 0]Understanding Uint8Array
The Uint8Array method in JavaScript creates a new Uint8Array typed array representing an array of 8-bit unsigned integers initialized to zero. 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 Uint8Array(length) or new Uint8Array(buffer, byteOffset?, length?). It accepts 1 parameter: length. When called, it returns a new uint8array instance. Understanding when and how to use Uint8Array() helps you write more expressive, readable code.
Common use cases for Uint8Array include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like int8array-constructor, uint16array-constructor, uint8clampedarray-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Uint8Array is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Int8ArrayCreates a new Int8Array typed array representing an array of 8-bit signed integers (range -128 to 127)
Uint16ArrayCreates a new Uint16Array typed array representing an array of 16-bit unsigned integers (range 0 to 65535)
Uint8ClampedArrayCreates a new Uint8ClampedArray typed array where values are clamped between 0 and 255 instead of wrapping
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.