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 703201 of Uint16Array

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Uint16Array
  • Revision title: Uint16Array
  • Revision id: 703201
  • Created:
  • Creator: arai
  • Is current revision? No
  • Comment iterator methods

Revision Content

{{JSRef("Global_Objects", "TypedArray", "Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array")}}

Summary

The Uint16Array typed array represents an array of 16-bit unsigned integers in the platform byte order. If control over byte order is needed, use {{jsxref("DataView")}} instead. The contents are initialized to 0. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).

Syntax

Uint16Array(length);
Uint16Array(typedArray);
Uint16Array(object);
Uint16Array(buffer [, byteOffset [, length]]);

For more information about the constructor syntax and the parameters, see TypedArray.

Properties

{{jsxref("TypedArray.BYTES_PER_ELEMENT", "Uint16Array.BYTES_PER_ELEMENT")}}
Returns a number value of the element size. 2 in the case of an Uint16Array.
Uint16Array.length
Length property whose value is 3.
{{jsxref("TypedArray.name", "Uint16Array.name")}}
Returns the string value of the constructor name. In the case of the Uint16Array type: "Uint16Array".
{{jsxref("TypedArray.prototype", "Uint16Array.prototype")}}
Prototype for the TypedArray objects.

Methods

Uint16Array.from()
See {{jsxref("Array.from()")}}. Not implemented, see {{bug(896608)}}.
Uint16Array.of()
See {{ jsxref("Array.of()")}}. Not implemented, see {{bug(896608)}}.

Uint16Array prototype

All Uint16Array objects inherit from {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}.

Properties

Uint16Array.prototype.constructor
Returns the function that created an instance's prototype. This is the Uint16Array constructor by default.
{{jsxref("TypedArray.prototype.buffer", "Uint16Array.prototype.buffer")}} {{readonlyInline}}
Returns the {{jsxref("ArrayBuffer")}} referenced by the Uint16Array Fixed at construction time and thus read only.
{{jsxref("TypedArray.prototype.byteLength", "Uint16Array.prototype.byteLength")}} {{readonlyInline}}
Returns the length (in bytes) of the Uint16Array from the start of its {{jsxref("ArrayBuffer")}}. Fixed at construction time and thus read only.
{{jsxref("TypedArray.prototype.byteOffset", "Uint16Array.prototype.byteOffset")}} {{readonlyInline}}
Returns the offset (in bytes) of the Uint16Array from the start of its {{jsxref("ArrayBuffer")}}. Fixed at construction time and thus read only.
{{jsxref("TypedArray.prototype.length", "Uint16Array.prototype.length")}} {{readonlyInline}}
Returns the number of elements hold in the Uint16Array. Fixed at construction time and thus read only.

Methods

{{jsxref("TypedArray.prototype.copyWithin()", "Uint16Array.prototype.copyWithin()")}}
See {{jsxref("Array.prototype.copyWithin()")}}.
{{jsxref("TypedArray.prototype.entries()", "Uint16Array.prototype.entries()")}}
Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
{{jsxref("TypedArray.prototype.keys()", "Uint16Array.prototype.keys()")}}
Returns a new Array Iterator that contains the keys for each index in the array.
{{jsxref("TypedArray.prototype.move()", "Uint16Array.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}
Former non-standard version of {{jsxref("TypedArray.prototype.copyWithin()", "Uint16Array.prototype.copyWithin()")}}.
{{jsxref("TypedArray.prototype.set()", "Uint16Array.prototype.set()")}}
Stores multiple values in the Uint16Array, reading input values from a specified array.
{{jsxref("TypedArray.prototype.subarray()", "Uint16Array.prototype.subarray()")}}
Returns a new Uint16Array from the given start and end element index.
{{jsxref("TypedArray.prototype.values()", "Uint16Array.prototype.values()")}}
Returns a new Array Iterator object that contains the values for each index in the array.
{{jsxref("TypedArray.prototype.@@iterator()", "Uint16Array.prototype[@@iterator]()")}}
Returns a new Array Iterator object that contains the values for each index in the array.

Examples

// From a length
var uint16 = new Uint16Array(2);
uint16[0] = 42;
console.log(uint16[0]); // 42
console.log(uint16.length); // 2
console.log(uint16.BYTES_PER_ELEMENT); // 2

// From an array
var arr = new Uint16Array([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint16Array([21, 31]);
var y = new Uint16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint16Array(buffer, 0, 4);

Specifications

Specification Status Comment
Typed Array Specification Obsolete Superseded by ECMAScript 6.
{{SpecName('ES6', '#table-45', 'TypedArray constructors')}} {{Spec2('ES6')}} Initial definition in an ECMA standard.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7.0 {{ CompatGeckoDesktop("2") }} 10 11.6 5.1
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 4.0 {{ CompatVersionUnknown() }} {{ CompatGeckoMobile("2") }} 10 11.6 4.2

See also

Revision Source

<div>
 {{JSRef("Global_Objects", "TypedArray", "Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array")}}</div>
<h2 id="Summary">Summary</h2>
<p>The <strong><code>Uint16Array</code></strong> typed array represents an array of 16-bit unsigned integers in the platform byte order. If control over byte order is needed, use {{jsxref("DataView")}} instead. The contents are initialized to <code>0</code>. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
Uint16Array(length);
Uint16Array(typedArray);
Uint16Array(object);
Uint16Array(buffer [, byteOffset [, length]]);</pre>
<p>For more information about the constructor syntax and the parameters, see <em><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Syntax">TypedArray</a></em>.</p>
<h2 id="Properties">Properties</h2>
<dl>
 <dt>
  {{jsxref("TypedArray.BYTES_PER_ELEMENT", "Uint16Array.BYTES_PER_ELEMENT")}}</dt>
 <dd>
  Returns a number value of the element size. <code>2</code> in the case of an <code>Uint16Array</code>.</dd>
 <dt>
  Uint16Array.length</dt>
 <dd>
  Length property whose value is 3.</dd>
 <dt>
  {{jsxref("TypedArray.name", "Uint16Array.name")}}</dt>
 <dd>
  Returns the string value of the constructor name. In the case of the <code>Uint16Array</code> type: "Uint16Array".</dd>
 <dt>
  {{jsxref("TypedArray.prototype", "Uint16Array.prototype")}}</dt>
 <dd>
  Prototype for the <em>TypedArray</em> objects.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
 <dt>
  Uint16Array.from()</dt>
 <dd>
  See {{jsxref("Array.from()")}}. Not implemented, see {{bug(896608)}}.</dd>
 <dt>
  Uint16Array.of()</dt>
 <dd>
  See {{ jsxref("Array.of()")}}. Not implemented, see {{bug(896608)}}.</dd>
</dl>
<h2 id="Boolean_instances" name="Boolean_instances"><code>Uint16Array</code> prototype</h2>
<p>All <code>Uint16Array</code> objects inherit from {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}.</p>
<h3 id="Properties_2">Properties</h3>
<dl>
 <dt>
  <code>Uint16Array.prototype.constructor</code></dt>
 <dd>
  Returns the function that created an instance's prototype. This is the <code>Uint16Array</code> constructor by default.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.buffer", "Uint16Array.prototype.buffer")}} {{readonlyInline}}</dt>
 <dd>
  Returns the {{jsxref("ArrayBuffer")}} referenced by the <code>Uint16Array</code> Fixed at construction time and thus <strong>read only</strong>.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.byteLength", "Uint16Array.prototype.byteLength")}} {{readonlyInline}}</dt>
 <dd>
  Returns the length (in bytes) of the <code>Uint16Array</code> from the start of its {{jsxref("ArrayBuffer")}}. Fixed at construction time and thus <strong>read only.</strong></dd>
 <dt>
  {{jsxref("TypedArray.prototype.byteOffset", "Uint16Array.prototype.byteOffset")}} {{readonlyInline}}</dt>
 <dd>
  Returns the offset (in bytes) of the <code>Uint16Array</code> from the start of its {{jsxref("ArrayBuffer")}}. Fixed at construction time and thus <strong>read only.</strong></dd>
 <dt>
  {{jsxref("TypedArray.prototype.length", "Uint16Array.prototype.length")}} {{readonlyInline}}</dt>
 <dd>
  Returns the number of elements hold in the <code>Uint16Array</code>. Fixed at construction time and thus <strong>read only.</strong></dd>
</dl>
<h3 id="Methods_2">Methods</h3>
<dl>
 <dt>
  {{jsxref("TypedArray.prototype.copyWithin()", "Uint16Array.prototype.copyWithin()")}}</dt>
 <dd>
  See {{jsxref("Array.prototype.copyWithin()")}}.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.entries()", "Uint16Array.prototype.entries()")}}</dt>
 <dd>
  Returns a new <code>Array Iterator</code> object that contains the key/value pairs for each index in the array.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.keys()", "Uint16Array.prototype.keys()")}}</dt>
 <dd>
  Returns a new <code>Array Iterator</code> that contains the keys for each index in the array.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.move()", "Uint16Array.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}</dt>
 <dd>
  Former non-standard version of {{jsxref("TypedArray.prototype.copyWithin()", "Uint16Array.prototype.copyWithin()")}}.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.set()", "Uint16Array.prototype.set()")}}</dt>
 <dd>
  Stores multiple values in the <code>Uint16Array</code>, reading input values from a specified array.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.subarray()", "Uint16Array.prototype.subarray()")}}</dt>
 <dd>
  Returns a new <code>Uint16Array</code> from the given start and end element index.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.values()", "Uint16Array.prototype.values()")}}</dt>
 <dd>
  Returns a new <code>Array Iterator</code> object that contains the values for each index in the array.</dd>
 <dt>
  {{jsxref("TypedArray.prototype.@@iterator()", "Uint16Array.prototype[@@iterator]()")}}</dt>
 <dd>
  Returns a new <code>Array Iterator</code> object that contains the values for each index in the array.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<pre class="brush: js">
// From a length
var uint16 = new Uint16Array(2);
uint16[0] = 42;
console.log(uint16[0]); // 42
console.log(uint16.length); // 2
console.log(uint16.BYTES_PER_ELEMENT); // 2

// From an array
var arr = new Uint16Array([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint16Array([21, 31]);
var y = new Uint16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint16Array(buffer, 0, 4);
</pre>
<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><a href="https://www.khronos.org/registry/typedarray/specs/latest/#7">Typed Array Specification</a></td>
   <td>Obsolete</td>
   <td><span><span>Superseded by ECMAScript 6.</span></span></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#table-45', 'TypedArray constructors')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard.</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>7.0</td>
    <td>{{ CompatGeckoDesktop("2") }}</td>
    <td>10</td>
    <td>11.6</td>
    <td>5.1</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</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>
   </tr>
  </tbody>
 </table>
</div>
<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