How to check if a variable is a typed array in javascript? How to check if a variable is a typed array in javascript? javascript javascript

How to check if a variable is a typed array in javascript?


ArrayBuffer.isView should help you out.

var data = [0,1,2]var dataBuffer = new ArrayBuffer( data )var dataBufferView = new Float32Array( data )ArrayBuffer.isView(data) //falseArrayBuffer.isView(dataBuffer) //falseArrayBuffer.isView(dataBufferView) //truedataBuffer instanceof ArrayBuffer //true


If you're happy with it being a Float32Array or a subclass of Float32Array and they'll be from the same realm (loosely, window) as the code you're checking, see Anton's answer using instanceof.

If you need to know that it's specifically a Float32Array and not a subclass (and its from the same realm), you could use yourObject.constructor === Float32Array:

if (yourObject.constructor === Float32Array) {     // It's a Float32Array}

Live example: