Course contentsShow
← JavaScript
Lesson 2333 of 2,72348. Working with Binary DataFree lesson

Creating ArrayBuffers

Using the ArrayBuffer constructor to allocate a specific number of bytes for binary data storage.

Creating ArrayBuffers

You'll learn how to allocate raw binary memory using the ArrayBuffer constructor.

What You're Creating

An ArrayBuffer is like reserving a blank parking lot — you're claiming a specific amount of space (measured in bytes) but not yet deciding what goes there. You create an ArrayBuffer by calling its constructor with a single argument: the number of bytes you want to allocate.

The Constructor Syntax

const buffer = new ArrayBuffer(byteLength);

The byteLength parameter tells JavaScript exactly how many bytes of memory to reserve. This creates a contiguous block of binary data, initialized to zeros.

Basic Examples

// Allocate 8 bytes of memory
const smallBuffer = new ArrayBuffer(8);

// Allocate 1024 bytes (1 KB)
const largeBuffer = new ArrayBuffer(1024);

// You can check the size
console.log(smallBuffer.byteLength); // 8
console.log(largeBuffer.byteLength); // 1024

Why Specify Byte Length?

Think of it like ordering a shipping container — you must tell the supplier exactly how many cubic feet you need upfront. Once created, an ArrayBuffer's size is fixed and immutable. You cannot resize it later. If you need more space, you must create a new ArrayBuffer.

Starting Point for Binary Work

The ArrayBuffer itself is just raw memory. You can't directly read or write individual bytes from it. Instead, you'll use typed arrays or DataView objects (which you've already learned about) to actually interpret and manipulate the binary data stored inside.

Key Takeaway: Use new ArrayBuffer(byteLength) to allocate a fixed-size block of raw binary memory measured in bytes — this is your foundation for all binary data operations.