Lodash _.isTypedArray() method
What you’ll learn
- How
_.isTypedArray(value)recognizes all nine native TypedArray views. - Why
ArrayBufferandDataVieware excluded. - How this differs from
Array.isArray. - Where TypedArray guards belong in binary protocol, WebGL, and Web Audio pipelines.
Prerequisites
You know that TypedArrays are fixed-element views over a backing ArrayBuffer.
- You have used at least one of
Uint8Array,Int16Array, orFloat32Array. - Try-it labs load lodash from the CDN.
Overview
Use _.isTypedArray before invoking element-aware operations on binary data—copying ranges, encoding/decoding samples, or feeding pixel buffers into GPU APIs.
Nine views
All native integer and float TypedArrays qualify.
Not the buffer
ArrayBuffer and DataView are excluded.
Binary-safe gate
Prevents arrays of mixed types from sneaking into byte pipelines.
Syntax
_.isTypedArray(value) - value: any value to test.
- Returns:
truewhen value is a TypedArray view; otherwisefalse.
Uint8Array, Float64Array, and friends
The lodash docs baseline: new Uint8Array returns true—and the same applies to every other native view.
import isTypedArray from "lodash/isTypedArray";
console.log(
"u8: " + isTypedArray(new Uint8Array([1, 2, 3])) + "\n" + // true (lodash docs)
"f64: " + isTypedArray(new Float64Array(4)) + "\n" + // true
"i16: " + isTypedArray(new Int16Array([0, -1])) // true
); ArrayBuffer and DataView
The raw buffer and its flexible accessor are not TypedArrays—both return false.
import isTypedArray from "lodash/isTypedArray";
const buf = new ArrayBuffer(8);
const view = new DataView(buf);
console.log(
"buf: " + isTypedArray(buf) + "\n" + // false
"dataView: " + isTypedArray(view) // false
); Plain arrays and nullish
Lodash docs show _.isTypedArray([]) as false—the bracket-literal array isn't a TypedArray.
import isTypedArray from "lodash/isTypedArray";
console.log(
"arr: " + isTypedArray([1, 2, 3]) + "\n" + // false (lodash docs)
"obj: " + isTypedArray({ length: 3 }) + "\n" + // false
"nullVal: " + isTypedArray(null) // false
); 📋 _.isTypedArray vs related checks
| API / pattern | Behavior |
|---|---|
_.isTypedArray(x) | Any native TypedArray view. |
Array.isArray(x) | Plain JS arrays only—TypedArrays fail. |
_.isArrayBuffer(x) | Detects the raw buffer; not a TypedArray. |
ArrayBuffer.isView(x) | Includes TypedArrays and DataView—a broader check. |
Pitfalls to avoid
Node.js Buffer
In Node, Buffer.from(...) is a subclass of Uint8Array, so it does pass this check.
Shared backing buffer
Two TypedArrays can share an ArrayBuffer; both pass the check but mutating one is visible to the other.
Custom subclasses
Classes extending a TypedArray inherit the internal tag and still register as TypedArrays.
❓ FAQ
Summary
- Purpose: guarantee a value is a TypedArray view before binary-safe operations.
- Remember:
ArrayBuffer,DataView, and ordinary arrays all returnfalse. - Next: explore more on Lodash _.isUndefined().
_.isTypedArray recognizes every native TypedArray view—Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, and Float64Array—but neither ArrayBuffer nor DataView qualify.
6 people found this page helpful
