Lodash _.isArrayBuffer() method
What you’ll learn
- How
_.isArrayBuffer(value)identifies trueArrayBufferinstances. - Why typed-array views and
DataViewreturnfalse. - How this differs from
_.isTypedArrayand nativeArrayBuffer.isView. - Practical checks when handling binary payloads in browsers and Node.
Prerequisites
Basic idea that binary APIs use ArrayBuffer plus typed arrays or DataView to read and write bytes.
- You can run snippets in a modern browser or Node with typed array support.
- You can open Try-it labs in the browser.
Overview
_.isArrayBuffer answers whether a value is a raw byte container before any typed view is attached. Reach for it when validating inputs from Web Crypto, WebSocket binary frames, fetch arrayBuffer(), or similar APIs.
Raw storage
Matches new ArrayBuffer(n)—including zero-length buffers.
Views are separate
Uint8Array / DataView wrap buffers; they are not buffers themselves.
Not duck typing
Fake objects with byteLength still fail unless they are real instances.
Syntax
_.isArrayBuffer(value) - value: any value to test.
- Returns:
trueifvalueis anArrayBuffer; otherwisefalse.
Plain ArrayBuffer instances
Any size—including 0 bytes—still constructs an ArrayBuffer, so _.isArrayBuffer stays true.
import isArrayBuffer from "lodash/isArrayBuffer";
isArrayBuffer(new ArrayBuffer(16)); // true
isArrayBuffer(new ArrayBuffer(0)); // true Typed arrays, DataView, and Arrays
Views interpret buffer memory; they are not classified as ArrayBuffers. Ordinary Arrays never qualify.
import isArrayBuffer from "lodash/isArrayBuffer";
var raw = new ArrayBuffer(8);
isArrayBuffer(new Uint8Array(raw)); // false
isArrayBuffer(new DataView(raw)); // false
isArrayBuffer([]); // false Same backing memory: buffer vs view
One underlying buffer can power many views; test the buffer when you need ownership of raw storage, and _.isTypedArray (or ArrayBuffer.isView) when any view will do.
import isArrayBuffer from "lodash/isArrayBuffer";
import isTypedArray from "lodash/isTypedArray";
var backing = new ArrayBuffer(16);
isArrayBuffer(backing); // true
isArrayBuffer(new Uint8Array(backing)); // false
isTypedArray(new Uint8Array(backing)); // true 📋 _.isArrayBuffer vs related checks
| API | Typical match |
|---|---|
_.isArrayBuffer(x) | ArrayBuffer instances. |
_.isTypedArray(x) | Uint8Array, Float64Array, etc. |
ArrayBuffer.isView(x) | Any typed array or DataView. |
_.isArray(x) | Ordinary JavaScript arrays only. |
Pitfalls to avoid
Assuming typed arrays are buffers
Grab .buffer from a typed array only when you know offsets align with what your API expects.
SharedArrayBuffer
Not treated as a classic ArrayBuffer; gate features carefully where workers need shared memory.
Buffer objects
Node’s Buffer is Uint8Array-based but distinct from user-created ArrayBuffer checks—use Buffer helpers when integrating native APIs.
❓ FAQ
Summary
- Purpose: detect raw
ArrayBufferinstances. - Exclude: typed-array views,
DataView, ordinary Arrays. - Next: explore more on Lodash _.isArrayLike().
An ArrayBuffer holds fixed-length raw bytes; Uint8Array, Float32Array, and DataView are separate constructors that view a buffer. Lodash follows that split—only the naked buffer passes _.isArrayBuffer.
6 people found this page helpful
