JavaScript Typed Array Reference Manual
- Previous Page JS Statements
- Next Page Window Object
JavaScript typed arrays
In Javascript, typed arrays are array-like buffers of binary data.
There is no JavaScript property or object named TypedArray, but properties and methods can be used with typed array objects:
Instance
const myArr = new Int8Array(10);
Object of typed arrays
Object | Data type | Range |
---|---|---|
Int8Array | Signed integer (byte) | -128/127 |
Uint8Array | Unsigned integer (8-bit byte) | 0/255 |
Uint8ClampedArray | Unsigned integer (8-bit byte) | 0/255 |
Int16Array | Short integer | -32768/32767 |
Uint16Array | Unsigned short integer | 0/65535 |
Int32Array | Signed long integer | -231/231-1 |
Uint32Array | Unsigned long integer | 0/232 |
Float32Array | Floating point - 7 significant digits | 1.2x10-38/3.4x1038 |
Float64Array | Double precision floating point - 16 significant digits | 5.0x10-324/1.8x10308 |
BigInt64Array | Signed big integer | -263/263-1 |
BigUint64Array | Unsigned big integer | 0/264 |
Explanation
Typed arrays are not arrays.
The isArray() on typed arrays returns false.
Typed arrays do not support many array methods (such as push and pop).
Typed arrays are array-like objects used to store binary data in memory.
Typed Array methods and properties
Method / Property | Description |
---|---|
BYTES_PER_ELEMENT | Calculate the property used to store the number of bytes for an element. |
fill() | Fill all elements with a value. |
find() | Return the first element that meets the condition. |
name | Return the name of the typed array. |
of() | Create a typed array from an array. |
some() | If an element meets the condition, it returns true. |
Comparison between Uint8Array and Uint8ClampedArray
The difference between Uint8Array and Uint8ClampedArray lies in how values are added.
If an element of Uint8ClampedArray is set to a value outside the range of 0-255, it will default to 0 or 255.
Typed arrays will only take the first 8 bits of a value.
Benefits of typed arrays
Typed arrays provide a method of handling binary data as effectively as arrays work in C.
Typed arrays are raw memory, so JavaScript can pass them directly to any function without converting the data to another representation.
Typed arrays are much faster than regular arrays and are used to pass data to functions that can use raw binary data (computer games, WebGL, Canvas, File API, Media API).
Browser API supports typed arrays
Fetch API instance
fetch(url) .then(request => request.arrayBuffer()) .then(arrayBuffer =>...);
Canvas instance
const canvas = document.getElementById('my_canvas'); const context = canvas.getContext('2d'); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); const uint8ClampedArray = imageData.data;
Browser Support
typedArray It is an ECMAScript6 (ES6) feature.
All modern browsers support ES6 (JavaScript 2015).
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
Internet Explorer 11 (and earlier versions) does not support typedArray.
- Previous Page JS Statements
- Next Page Window Object