A TypedArray object describes an array-like view of an underlying binary data buffer. There is no global property named TypedArray
, nor is there a directly visible TypedArray
constructor. Instead, there are a number of different global properties, whose values are typed array constructors for specific element types, listed below. On the following pages you will find common properties and methods that can be used with any typed array containing elements of any type.
new TypedArray(); // new in ES2017 new TypedArray(length); new TypedArray(typedArray); new TypedArray(object); new TypedArray(buffer [, byteOffset [, length]]); where TypedArray() is one of: Int8Array(); Uint8Array(); Uint8ClampedArray(); Int16Array(); Uint16Array(); Int32Array(); Uint32Array(); Float32Array(); Float64Array();
length
argument, an internal array buffer is created in memory, of size length multiplied by BYTES_PER_ELEMENT bytes, containing zeros.typedArray
argument, which can be an object of any of the typed array types (such as Int32Array
), the typedArray
gets copied into a new typed array. Each value in typedArray
is converted to the corresponding type of the constructor before being copied into the new array. The length of the new typed array will be same as the length of the typedArray
argument.object
argument, a new typed array is created as if by the TypedArray.from()
method.buffer
, and optionally a byteOffset
and a length
argument, a new typed array view is created that views the specified ArrayBuffer
. The byteOffset
and length
parameters specify the memory range that will be exposed by the typed array view. If both are omitted, all of buffer
is viewed; if only length
is omitted, the remainder of buffer
is viewed.ECMAScript 2015 defines a TypedArray constructor that serves as the [[Prototype]]
of all TypedArray constructors. This constructor is not directly exposed: there is no global %TypedArray%
or TypedArray
property. It is only directly accessible through Object.getPrototypeOf(Int8Array)
and similar. All TypedArrays constructors inherit common properties from the %TypedArray%
constructor function. Additionally, all typed array prototypes (TypedArray.prototype
) have %TypedArray%.prototype
as their [[Prototype]]
.
The %TypedArray%
constructor on its own is not particularly useful. Calling it or using it in a new
expression will throw a TypeError
, except when used during object creation in JS engines that support subclassing. There are at present no such engines, so %TypedArray%
is only useful to polyfill functions or properties onto all TypedArray constructors.
When creating an instance of a TypedArray (e.g. Int8Array
), an array buffer is created internally in memory or, if an ArrayBuffer
object is given as constructor argument, then this is used instead. The buffer address is saved as an internal property of the instance and all the methods of %TypedArray
%.prototype
, i.e. set value and get value etc., operate on that array buffer address.
You can reference elements in the array using standard array index syntax (that is, using bracket notation). However, getting or setting indexed properties on typed arrays will not search in the prototype chain for this property, even when the indices are out of bound. Indexed properties will consult the ArrayBuffer
and will never look at object properties. You can still use named properties, just like with all objects.
// Setting and getting using standard array syntax var int16 = new Int16Array(2); int16[0] = 42; console.log(int16[0]); // 42 // Indexed properties on prototypes are not consulted (Fx 25) Int8Array.prototype[20] = 'foo'; (new Int8Array(32))[20]; // 0 // even when out of bound Int8Array.prototype[20] = 'foo'; (new Int8Array(8))[20]; // undefined // or with negative integers Int8Array.prototype[-1] = 'foo'; (new Int8Array(8))[-1]; // undefined // Named properties are allowed, though (Fx 30) Int8Array.prototype.foo = 'bar'; (new Int8Array(32)).foo; // "bar"
Type | Value Range | Size in bytes | Description | Web IDL type | Equivalent C type |
---|---|---|---|---|---|
Int8Array | -128 to 127 | 1 | 8-bit two's complement signed integer | byte | int8_t |
Uint8Array | 0 to 255 | 1 | 8-bit unsigned integer | octet | uint8_t |
Uint8ClampedArray | 0 to 255 | 1 | 8-bit unsigned integer (clamped) | octet | uint8_t |
Int16Array | -32768 to 32767 | 2 | 16-bit two's complement signed integer | short | int16_t |
Uint16Array | 0 to 65535 | 2 | 16-bit unsigned integer | unsigned short | uint16_t |
Int32Array | -2147483648 to 2147483647 | 4 | 32-bit two's complement signed integer | long | int32_t |
Uint32Array | 0 to 4294967295 | 4 | 32-bit unsigned integer | unsigned long | uint32_t |
Float32Array | 1.2x10-38 to 3.4x1038 | 4 | 32-bit IEEE floating point number ( 7 significant digits e.g. 1.1234567) | unrestricted float | float |
Float64Array | 5.0x10-324 to 1.8x10308 | 8 | 64-bit IEEE floating point number (16 significant digits e.g. 1.123...15) | unrestricted double | double |
TypedArray.BYTES_PER_ELEMENT
TypedArray.name
get TypedArray[@@species]
TypedArray.prototype
TypedArray.from()
Array.from()
.TypedArray.of()
Array.of()
.All TypedArrays inherit from TypedArray.prototype
.
TypedArray.prototype.constructor
TypedArray.prototype.buffer
Read only
ArrayBuffer
referenced by the typed array. Fixed at construction time and thus read only.TypedArray.prototype.byteLength
Read only
TypedArray.prototype.byteOffset
Read only
ArrayBuffer
. Fixed at construction time and thus read only.
TypedArray.prototype.length
Read only
TypedArray.prototype.copyWithin()
Array.prototype.copyWithin()
.TypedArray.prototype.entries()
Array Iterator
object that contains the key/value pairs for each index in the array. See also Array.prototype.entries()
.TypedArray.prototype.every()
Array.prototype.every()
.TypedArray.prototype.fill()
Array.prototype.fill()
.TypedArray.prototype.filter()
Array.prototype.filter()
.TypedArray.prototype.find()
undefined
if not found. See also Array.prototype.find()
.TypedArray.prototype.findIndex()
Array.prototype.findIndex()
.TypedArray.prototype.forEach()
Array.prototype.forEach()
.TypedArray.prototype.includes()
true
or false
as appropriate. See also Array.prototype.includes()
.TypedArray.prototype.indexOf()
Array.prototype.indexOf()
.TypedArray.prototype.join()
Array.prototype.join()
.TypedArray.prototype.keys()
Array Iterator
that contains the keys for each index in the array. See also Array.prototype.keys()
.TypedArray.prototype.lastIndexOf()
Array.prototype.lastIndexOf()
.TypedArray.prototype.map()
Array.prototype.map()
.TypedArray.prototype.move()
Unimplemented
TypedArray.prototype.copyWithin()
.TypedArray.prototype.reduce()
Array.prototype.reduce()
.TypedArray.prototype.reduceRight()
Array.prototype.reduceRight()
.TypedArray.prototype.reverse()
Array.prototype.reverse()
.TypedArray.prototype.set()
TypedArray.prototype.slice()
Array.prototype.slice()
.TypedArray.prototype.some()
Array.prototype.some()
.TypedArray.prototype.sort()
Array.prototype.sort()
.TypedArray.prototype.subarray()
TypedArray.prototype.values()
Array Iterator
object that contains the values for each index in the array. See also Array.prototype.values()
.TypedArray.prototype.toLocaleString()
Array.prototype.toLocaleString()
.TypedArray.prototype.toString()
Array.prototype.toString()
.TypedArray.prototype[@@iterator]()
Array Iterator
object that contains the values for each index in the array.Many of the methods used in Typed Arrays can be polyfilled using the methods present in regular Javascript Arrays. The following snippet of JavaScript demonstrates how you might polyfill any missing Typed Array methods.
var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array]; for (var k in typedArrayTypes) for (var v in Array.prototype) if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) typedArrayTypes[k].prototype[v] = Array.prototype[v];
Specification | Status | Comment |
---|---|---|
Typed Array Specification | Obsolete | Defined as TypedArray and ArrayBufferView interface with typed array view types. Superseded by ECMAScript 2015. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'TypedArray Objects' in that specification. | Standard | Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required. |
ECMAScript Latest Draft (ECMA-262) The definition of 'TypedArray Objects' in that specification. | Draft | ECMAScript 2017 changed the TypedArray constructor to use the ToIndex operation and allows constructors with no arguments. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 7 | Yes | 4 | 10 | 11.6 | 5.1 |
Constructor without arguments | ? | ? | 55 | 10 | ? | ? |
Indexed properties not consulting prototype | Yes
|
? | 25 | 10
|
? | ? |
Iterable in constructor | ? | ? | 52 | 10 | ? | ? |
Named properties | Yes | ? | 30 | 10 | ? | ? |
TypedArray() without new throws |
? | ? | 44 | No | ? | ? |
BYTES_PER_ELEMENT |
7 | Yes | 4 | 10 | 11.6 | 5.1 |
buffer |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
byteLength |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
byteOffset |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
copyWithin |
45 | 14 | 34 | No | 36 | No |
entries |
45 | 14 | 37 | No | 36 | No |
every |
45 | 14 | 37 | No | 36 | No |
fill |
45 | 14 | 37 | No | 36 | No |
filter |
45 | 14 | 38 | No | No | No |
find |
Yes | 14 | 37 | No | No | No |
findIndex |
Yes | 14 | 37 | No | No | No |
forEach |
Yes | 14 | 38 | No | ? | 10 |
from |
45 | 14 | 38 | No | No | 10 |
includes |
47 | 14 | 43 | No | 34 | 10 |
indexOf |
45 | 14 | 37
|
No | 32 | No |
join |
Yes | 14 | 37 | No | No | No |
keys |
Yes | 14 | 37 | No | No | 10 |
lastIndexOf |
Yes | 14 | 37
|
No | Yes | 10 |
length |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
map |
Yes | 14 | 38 | No | No | No |
move
|
No | No | 16 — 34
|
No | No | No |
name |
7 | Yes | 4 | 10 | 11.6 | 5.1 |
of |
45 | 14 | 38 | No | No | No |
prototype |
7 | Yes | 4 | 10 | 11.6 | 5.1 |
reduce |
45 | 14 | 37 | No | 32 | 10 |
reduceRight |
45 | 14 | 37 | No | 32 | 10 |
reverse |
45 | 14 | 37 | No | 32 | 10 |
set |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
slice |
45 | 14 | 38 | No | ? | ? |
some |
45 | 14 | 37 | No | 32 | 10 |
sort |
Yes | 14 | 46 | No | Yes | ? |
subarray |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
toLocaleString |
Yes | ? | 51 | 10 | Yes | Yes |
toString |
Yes | ? | 51 | No | Yes | Yes |
values |
Yes | 14 | 37 | No | 26 | 10 |
@@iterator |
Yes | ? | 36
|
No | Yes | Yes |
@@species |
Yes | ? | 48 | No | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
Constructor without arguments | ? | ? | ? | 55 | ? | ? | ? |
Indexed properties not consulting prototype | Yes
|
Yes
|
? | 25 | ? | ? | Yes |
Iterable in constructor | ? | ? | ? | 52 | ? | ? | ? |
Named properties | Yes | Yes | ? | 30 | ? | ? | Yes |
TypedArray() without new throws |
? | ? | ? | 44 | ? | ? | ? |
BYTES_PER_ELEMENT |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
buffer |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
byteLength |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
byteOffset |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
copyWithin |
No | No | ? | 34 | No | No | No |
entries |
No | Yes | ? | 37 | No | No | Yes |
every |
No | Yes | ? | 37 | No | No | Yes |
fill |
No | Yes | ? | 37 | No | No | Yes |
filter |
No | Yes | ? | 38 | No | No | Yes |
find |
No | No | ? | 37 | No | No | No |
findIndex |
No | No | ? | 37 | No | No | No |
forEach |
No | Yes | ? | 38 | ? | ? | Yes |
from |
No | No | ? | 38 | No | 10 | No |
includes |
No | 47 | ? | 43 | 34 | 10 | 5.0 |
indexOf |
No | 45 | Yes | 37
|
32 | No | 5.0 |
join |
No | Yes | Yes | 37 | No | No | Yes |
keys |
No | Yes | Yes | 37 | No | 10 | Yes |
lastIndexOf |
No | Yes | Yes | 37
|
Yes | 10 | Yes |
length |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
map |
No | Yes | Yes | 38 | No | No | Yes |
move
|
No | No | No | 38 — 34
|
No | No | No |
name |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
of |
No | No | ? | 38 | No | No | No |
prototype |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
reduce |
? | ? | Yes | 37 | No | 10 | ? |
reduceRight |
? | ? | Yes | 37 | No | 10 | ? |
reverse |
? | ? | Yes | 37 | No | 10 | ? |
set |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
slice |
? | ? | ? | 38 | ? | ? | ? |
some |
? | ? | ? | 37 | No | 10 | ? |
sort |
? | ? | ? | 46 | ? | ? | ? |
subarray |
4 | Yes | Yes | 4 | 11.6 | 4.2 | Yes |
toLocaleString |
Yes | Yes | ? | 51 | Yes | Yes | Yes |
toString |
Yes | Yes | ? | 51 | Yes | Yes | Yes |
values |
Yes | Yes | Yes | 37 | Yes | 10 | Yes |
@@iterator |
Yes | Yes | ? | 36
|
Yes | Yes | Yes |
@@species |
? | ? | ? | 48 | ? | ? | ? |
Server | |
---|---|
Node.js | |
Basic support | Yes |
Constructor without arguments | ? |
Indexed properties not consulting prototype | ?
|
Iterable in constructor | 4.0.0 |
Named properties | ? |
TypedArray() without new throws |
0.12 |
BYTES_PER_ELEMENT |
Yes |
buffer |
Yes |
byteLength |
Yes |
byteOffset |
Yes |
copyWithin |
4.0.0 |
entries |
0.12 |
every |
4.0.0 |
fill |
4.0.0 |
filter |
4.0.0 |
find |
4.0.0 |
findIndex |
4.0.0 |
forEach |
4.0.0 |
from |
4.0.0 |
includes |
6.0.0
|
indexOf |
4.0.0 |
join |
4.0.0 |
keys |
0.12 |
lastIndexOf |
4.0.0 |
length |
Yes |
map |
4.0.0 |
move
|
No |
name |
Yes |
of |
4.0.0 |
prototype |
Yes |
reduce |
4.0.0 |
reduceRight |
4.0.0 |
reverse |
4.0.0 |
set |
? |
slice |
4.0.0 |
some |
4.0.0 |
sort |
4.0.0 |
subarray |
0.12 |
toLocaleString |
? |
toString |
? |
values |
0.12 |
@@iterator |
0.12 |
@@species |
6.5.0
|
Starting with ECMAScript 2015, TypedArray
constructors require to be constructed with a new
operator. Calling a TypedArray
constructor as a function without new
, will throw a TypeError
from now on.
var dv = Int8Array([1, 2, 3]); // TypeError: calling a builtin Int8Array constructor // without new is forbidden
var dv = new Int8Array([1, 2, 3]);
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray