ArrayBuffer: allocate and inspect bytes
Quick answer
An ArrayBuffer holds a region of binary memory. It has a byte length, but does not expose numeric element indexing. A typed array or DataView interprets its bytes.
How it works
Use new ArrayBuffer(byteLength) to allocate zero-initialized bytes. byteLength reports current bytes. slice(start,end) copies a byte range with an exclusive end. Ordinary fixed-length buffers cannot grow; resizable buffers are a separate capability. Allocate only bounded sizes from validated input.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const b=new ArrayBuffer(4);console.log(b.byteLength,JSON.stringify([...new Uint8Array(b)]));})()Expected console output:
4 [0,0,0,0]Allocation initializes bytes to zero.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const a=new Uint8Array([1,2,3]);const c=a.buffer.slice(1,3);a[1]=9;console.log(JSON.stringify([...new Uint8Array(c)]));})()Expected console output:
[2,3]The copied buffer is independent.
Common mistakes
Assigning buffer[0] sets an ordinary property, not a byte. Use a view instead.
Practice
Allocate four bytes and copy the last two; inspect lengths and mutate the original through a byte view.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ArrayBuffer: allocate and inspect bytes — Knowledge Check
Apply the concept and reason about local example output.
Which unit does ArrayBuffer.byteLength report?
How should raw buffer bytes be accessed?
What is the end index of ArrayBuffer.slice?
What is the expected console output of ArrayBuffer: allocate and inspect bytes example 1?
(async () => {const b=new ArrayBuffer(4);console.log(b.byteLength,JSON.stringify([...new Uint8Array(b)]));})()What is the expected console output of ArrayBuffer: allocate and inspect bytes example 2?
(async () => {const a=new Uint8Array([1,2,3]);const c=a.buffer.slice(1,3);a[1]=9;console.log(JSON.stringify([...new Uint8Array(c)]));})()