Typed Arrays Recognized by Lodash isTypedArray
The _.isTypedArray method in Lodash is a utility
function used to check whether a given value is a typed array object.
This guide outlines all the specific typed array types that
_.isTypedArray identifies, explains how the method
distinguishes them from standard arrays, and details related binary data
structures that the function deliberately ignores.
Supported Typed Arrays
In JavaScript, typed arrays are array-like objects that provide a
mechanism for reading and writing raw binary data in memory buffers.
Lodash's _.isTypedArray evaluates the underlying object's
internal tag to determine its type.
The method returns true for instances of the following
11 standard typed arrays:
- Int8Array: 8-bit two's complement signed integers.
- Uint8Array: 8-bit unsigned integers.
- Uint8ClampedArray: 8-bit unsigned integers clamped to 0-255 (commonly used in HTML canvas imaging).
- Int16Array: 16-bit two's complement signed integers.
- Uint16Array: 16-bit unsigned integers.
- Int32Array: 32-bit two's complement signed integers.
- Uint32Array: 32-bit unsigned integers.
- Float32Array: 32-bit IEEE floating-point numbers.
- Float64Array: 64-bit IEEE floating-point numbers.
- BigInt64Array: 64-bit two's complement signed integers (supported in ECMAScript 2020+ environments).
- BigUint64Array: 64-bit unsigned integers (supported in ECMAScript 2020+ environments).
Example Usage
const _ = require('lodash');
// Returns true
_.isTypedArray(new Int8Array());
_.isTypedArray(new Uint8Array(8));
_.isTypedArray(new Float64Array([1.5, 2.5]));
_.isTypedArray(new BigInt64Array([10n, 20n]));Objects Not Recognized by _.isTypedArray
While several other JavaScript structures work closely with binary
data, they are not typed arrays and will return false:
- Standard Arrays (
[]ornew Array()): Regular JavaScript arrays have dynamic sizing and can store arbitrary types, meaning they lack typed array memory layouts. ArrayBuffer: Represents the underlying raw binary data buffer itself, not the view used to access it.SharedArrayBuffer: Represents shared raw binary memory.DataView: A low-level interface for reading and writing multiple number types in anArrayBufferwithout considering platform endianness, but it is classified separately from typed array views.
// Returns false
_.isTypedArray([]);
_.isTypedArray(new Array(10));
_.isTypedArray(new ArrayBuffer(16));
_.isTypedArray(new DataView(new ArrayBuffer(16)));