Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1117131 of TypedArray

  • Revision slug: Web/JavaScript/Reference/Global_Objects/TypedArray
  • Revision title: TypedArray
  • Revision id: 1117131
  • Created:
  • Creator: ariyankhan
  • Is current revision? No
  • Comment

Revision Content

{{JSRef}}

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.

Syntax

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();

Parameters

length
When called with a length argument, a internal array buffer is created in memory of size length multiplied by BYTES_PER_ELEMENT bytes containing 0 value.
typedArray
When called with a 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. Then length of the new typedArray object will be same of length of the typedArray arguemnt.
object
When called with an object argument, a new typed array is created as if by the TypedArray.from() method.
buffer, byteOffset, length
When called with a buffer, and optionally a byteOffset and a length argument, a new typed array view is created that views the specified {{jsxref("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.

Description

ECMAScript 6 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 a TypedArray instance (i.e. instance of Int8Array or similar), a array buffer is created internally (if ArrayBuffer object is present as constructor argument then this array buffer is used) in memory and this buffer address is saved as internal property of that insatnces, and all the methods of %TypedArray%.prototype uses that array buffer address to operate on i.e. set value and get value etc.

Property access

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 {{jsxref("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"

TypedArray objects

Type Size in bytes Description Web IDL type Equivalent C type
{{jsxref("Int8Array")}} 1 8-bit two's complement signed integer byte int8_t
{{jsxref("Uint8Array")}} 1 8-bit unsigned integer octet uint8_t
{{jsxref("Uint8ClampedArray")}} 1 8-bit unsigned integer (clamped) octet uint8_t
{{jsxref("Int16Array")}} 2 16-bit two's complement signed integer short int16_t
{{jsxref("Uint16Array")}} 2 16-bit unsigned integer unsigned short uint16_t
{{jsxref("Int32Array")}} 4 32-bit two's complement signed integer long int32_t
{{jsxref("Uint32Array")}} 4 32-bit unsigned integer unsigned long uint32_t
{{jsxref("Float32Array")}} 4 32-bit IEEE floating point number unrestricted float float
{{jsxref("Float64Array")}} 8 64-bit IEEE floating point number unrestricted double double

Properties

{{jsxref("TypedArray.BYTES_PER_ELEMENT")}}
Returns a number value of the element size for the different typed array objects.
TypedArray.length
Length property whose value is 3.
{{jsxref("TypedArray.name")}}
Returns the string value of the constructor name. E.g "Int8Array".
{{jsxref("TypedArray.@@species", "get TypedArray[@@species]")}}
The constructor function that is used to create derived objects.
{{jsxref("TypedArray.prototype")}}
Prototype for the TypedArray objects.

Methods

{{jsxref("TypedArray.from()")}}
Creates a new typed array from an array-like or iterable object. See also {{jsxref("Array.from()")}}.
{{jsxref("TypedArray.of()")}}
Creates a new typed array with a variable number of arguments. See also {{jsxref("Array.of()")}}.

TypedArray prototype

All TypedArrays inherit from {{jsxref("TypedArray.prototype")}}.

Properties

{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Properties')}}

Methods

{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Methods')}}

Specifications

Specification Status Comment
{{SpecName('Typed Array')}} {{Spec2('Typed Array')}} Defined as TypedArray and ArrayBufferView interface with typed array view types. Superseded by ECMAScript 6.
{{SpecName('ES6', '#sec-typedarray-objects', 'TypedArray Objects')}} {{Spec2('ES6')}} Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required.
{{SpecName('ESDraft', '#sec-typedarray-objects', 'TypedArray Objects')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(7.0)}} {{CompatGeckoDesktop("2")}} 10 11.6 5.1
Indexed properties not consulting prototype {{CompatVersionUnknown}} [1] {{CompatGeckoDesktop("25")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Named properties {{CompatVersionUnknown}} {{CompatGeckoDesktop("30")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
new is required {{CompatUnknown}} {{CompatGeckoDesktop("44")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 4.0 {{CompatVersionUnknown}} {{ CompatGeckoMobile("2") }} 10 11.6 4.2 {{CompatVersionUnknown}}
Indexed properties not consulting prototype {{CompatUnknown}} {{CompatVersionUnknown}} [1] {{ CompatGeckoMobile("25") }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} [1]
Named properties {{CompatUnknown}} {{CompatVersionUnknown}} {{ CompatGeckoMobile("30") }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
new is required {{CompatUnknown}} {{CompatUnknown}} {{ CompatGeckoMobile("44") }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[1] -1 and similar are not considered as indexed properties and therefore return the value of the prototype property.

Compatibility notes

Starting with ECMAScript 2015 (ES6), TypedArray constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling a TypedArray constructor as a function without new, will throw a {{jsxref("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]);

See also

Revision Source

<div>{{JSRef}}</div>

<p>A <strong><em>TypedArray</em></strong> object describes an array-like view of an underlying <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer">binary data buffer</a>. There is no global property named <code>TypedArray</code>, nor is there a directly visible <code>TypedArray</code> constructor.&nbsp; 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.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
new<em> TypedArray</em>(length);
new <em>TypedArray</em>(typedArray);
new <em>TypedArray</em>(object);
new <em>TypedArray</em>(buffer [, byteOffset [, length]]);

where <em>TypedArray()</em> is one of:

Int8Array();
Uint8Array();
Uint8ClampedArray();
Int16Array();
Uint16Array();
Int32Array();
Uint32Array();
Float32Array();
Float64Array();
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt>length</dt>
 <dd>When called with a <code>length</code> argument, a internal array buffer is created in memory of size <em>length multiplied by BYTES_PER_ELEMENT</em>&nbsp;bytes containing 0 value.</dd>
 <dt>typedArray</dt>
 <dd>When called with a <code>typedArray</code> argument, which can be an object of any of the typed array types (such as <code>Int32Array</code>), the <code>typedArray</code> gets copied into a new typed array. Each value in <code>typedArray</code> is converted to the corresponding type of the constructor before being copied into the new array. Then length of the new typedArray object will be same of length of the&nbsp;typedArray arguemnt.</dd>
 <dt>object</dt>
 <dd>When called with an <code>object</code> argument, a new typed array is created as if by the <code><em>TypedArray</em>.from()</code> method.</dd>
 <dt>buffer, byteOffset, length</dt>
 <dd>When called with a <code>buffer</code>, and optionally a <code>byteOffset</code> and a <code>length</code> argument, a new typed array view is created that views the specified {{jsxref("ArrayBuffer")}}. The <code>byteOffset</code> and <code>length</code> parameters specify the memory range that will be exposed by the typed array view.&nbsp; If both are omitted, all of <code>buffer</code> is viewed; if only <code>length</code> is omitted, the remainder of <code>buffer</code> is viewed.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>ECMAScript 6 defines a <em>TypedArray</em> constructor that serves as the <code>[[Prototype]]</code> of all <em>TypedArray</em> constructors.&nbsp; This constructor is not directly exposed: there is no global <code>%TypedArray%</code> or <code>TypedArray</code> property.&nbsp; It is only directly accessible through <code>Object.getPrototypeOf(Int8Array)</code> and similar.&nbsp; All <em>TypedArray</em>s constructors inherit common properties from the <code>%TypedArray%</code> constructor function.&nbsp; Additionally, all typed array prototypes (<em>TypedArray</em><code>.prototype</code>) have <code>%TypedArray%.prototype</code> as their <code>[[Prototype]]</code>.</p>

<p>The <code>%TypedArray%</code> constructor on its own is not particularly useful.&nbsp; Calling it or using it in a <code>new</code> expression will throw a <code>TypeError</code>, except when used during object creation in JS engines that support subclassing.&nbsp; There are at present no such engines, so <code>%TypedArray%</code> is only useful to polyfill functions or properties onto all <em>TypedArray</em> constructors.</p>

<p>When creating a&nbsp;<em>TypedArray </em>instance&nbsp;(i.e. instance of Int8Array or similar), a array buffer is created internally (if ArrayBuffer object is present as constructor argument then this array buffer is used) in memory and this buffer address is saved as internal property of that insatnces, and all the methods of&nbsp;%<code>TypedArray</code>%.<code>prototype </code>uses that array buffer address to operate on i.e. set value and get value etc.</p>

<h3 id="Property_access">Property access</h3>

<p>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 {{jsxref("ArrayBuffer")}} and will never look at object properties. You can still use named properties, just like with all objects.</p>

<pre class="brush: js">
// 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"</pre>

<h2 id="TypedArray_objects">TypedArray objects</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Type</td>
   <td class="header">Size in bytes</td>
   <td class="header">Description</td>
   <td class="header">Web IDL type</td>
   <td class="header">Equivalent C type</td>
  </tr>
  <tr>
   <td>{{jsxref("Int8Array")}}</td>
   <td>1</td>
   <td>8-bit two's complement signed integer</td>
   <td><code>byte</code></td>
   <td><code>int8_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Uint8Array")}}</td>
   <td>1</td>
   <td>8-bit unsigned integer</td>
   <td><code>octet</code></td>
   <td><code>uint8_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Uint8ClampedArray")}}</td>
   <td>1</td>
   <td>8-bit unsigned integer (clamped)</td>
   <td><code>octet</code></td>
   <td><code>uint8_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Int16Array")}}</td>
   <td>2</td>
   <td>16-bit two's complement signed integer</td>
   <td><code>short</code></td>
   <td><code>int16_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Uint16Array")}}</td>
   <td>2</td>
   <td>16-bit unsigned integer</td>
   <td><code>unsigned short</code></td>
   <td><code>uint16_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Int32Array")}}</td>
   <td>4</td>
   <td>32-bit two's complement signed integer</td>
   <td><code>long</code></td>
   <td><code>int32_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Uint32Array")}}</td>
   <td>4</td>
   <td>32-bit unsigned integer</td>
   <td><code>unsigned long</code></td>
   <td><code>uint32_t</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Float32Array")}}</td>
   <td>4</td>
   <td>32-bit IEEE&nbsp;floating point number</td>
   <td><code>unrestricted float</code></td>
   <td><code>float</code></td>
  </tr>
  <tr>
   <td>{{jsxref("Float64Array")}}</td>
   <td>8</td>
   <td>64-bit IEEE floating point number</td>
   <td><code>unrestricted double</code></td>
   <td><code>double</code></td>
  </tr>
 </tbody>
</table>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{jsxref("TypedArray.BYTES_PER_ELEMENT")}}</dt>
 <dd>Returns a number value of the element size for the different typed array objects.</dd>
 <dt><em>TypedArray</em>.length</dt>
 <dd>Length property whose value is 3.</dd>
 <dt>{{jsxref("TypedArray.name")}}</dt>
 <dd>Returns the string value of the constructor name. E.g "Int8Array".</dd>
 <dt>{{jsxref("TypedArray.@@species", "get TypedArray[@@species]")}}</dt>
 <dd>The constructor function that is used to create derived objects.</dd>
 <dt>{{jsxref("TypedArray.prototype")}}</dt>
 <dd>Prototype for the <em>TypedArray</em> objects.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{jsxref("TypedArray.from()")}}</dt>
 <dd>Creates a new typed array from an array-like or iterable object. See also {{jsxref("Array.from()")}}.</dd>
 <dt>{{jsxref("TypedArray.of()")}}</dt>
 <dd>Creates a new typed array with a variable number of arguments. See also {{jsxref("Array.of()")}}.</dd>
</dl>

<h2 id="TypedArray_prototype">TypedArray prototype</h2>

<p>All <em>TypedArray</em>s inherit from {{jsxref("TypedArray.prototype")}}.</p>

<h3 id="Properties_2">Properties</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Properties')}}</p>

<h3 id="Methods_2">Methods</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Methods')}}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Typed Array')}}</td>
   <td>{{Spec2('Typed Array')}}</td>
   <td>Defined as <code>TypedArray</code> and <code>ArrayBufferView</code> interface with typed array view types. Superseded by ECMAScript 6.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-typedarray-objects', 'TypedArray Objects')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that <code>new</code> is required.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-typedarray-objects', 'TypedArray Objects')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(7.0)}}</td>
   <td>{{CompatGeckoDesktop("2")}}</td>
   <td>10</td>
   <td>11.6</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td>Indexed properties not consulting prototype</td>
   <td>{{CompatVersionUnknown}} [1]</td>
   <td>{{CompatGeckoDesktop("25")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Named properties</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("30")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>new</code> is required</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("44")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile("2") }}</td>
   <td>10</td>
   <td>11.6</td>
   <td>4.2</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Indexed properties not consulting prototype</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}} [1]</td>
   <td>{{ CompatGeckoMobile("25") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}} [1]</td>
  </tr>
  <tr>
   <td>Named properties</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile("30") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>new</code> is required</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoMobile("44") }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>-1</code> and similar are not considered as indexed properties and therefore return the value of the prototype property.</p>

<h2 id="Compatibility_notes">Compatibility notes</h2>

<p>Starting with ECMAScript 2015 (ES6), <code>TypedArray</code> constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling a <code>TypedArray</code> constructor as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p>

<pre class="brush: js example-bad">
var dv = Int8Array([1, 2, 3]);
// TypeError: calling a builtin Int8Array constructor 
// without new is forbidden</pre>

<pre class="brush: js example-good">
var dv = new Int8Array([1, 2, 3]);</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li>
 <li>{{jsxref("ArrayBuffer")}}</li>
 <li>{{jsxref("DataView")}}</li>
</ul>
Revert to this revision