ArrayBuffer

ArrayBuffer

Creates a new ArrayBuffer of the given length in bytes, with contents initialized to zero

Syntax

JavaScript
new ArrayBuffer(byteLength)

Parameters

ParameterTypeDescription
byteLengthnumberThe size in bytes of the array buffer to create

Return Value

A new ArrayBuffer object of the specified size

Examples

Basic Usage
const buffer = new ArrayBuffer(16)
console.log(buffer.byteLength) // 16
Practical Example
const buffer = new ArrayBuffer(8)
const view = new Float64Array(buffer)
view[0] = Math.PI
console.log(new Uint8Array(buffer))
Advanced Usage
async function readFileAsBuffer(file: File): Promise<ArrayBuffer> {
  return file.arrayBuffer()
}

Understanding ArrayBuffer

The ArrayBuffer method in JavaScript creates a new ArrayBuffer of the given length in bytes, with contents initialized to zero. It belongs to the ArrayBuffer object and is one of the most widely used methods for working with arraybuffer values in modern JavaScript and TypeScript applications.

The method signature is new ArrayBuffer(byteLength). It accepts 1 parameter: byteLength. When called, it returns a new arraybuffer object of the specified size. Understanding when and how to use ArrayBuffer() helps you write more expressive, readable code.

Common use cases for ArrayBuffer include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like arraybuffer-slice, dataview-constructor, uint8array-constructor, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for ArrayBuffer 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

More ArrayBuffer Methods

Other methods in the ArrayBuffer object

Related Tools

More ArrayBuffer Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.