JS TutorialLesson #261

Typed Array Methods

The from()method creates a new typed array from any iterable object:

The from() Method

The from()method creates a new typed array from any iterable object:

Examples

Example Code

javascript
Try it Yourself
const myArr = Int16Array.from("1234567890");

The of() Method

The of() method creates a new typed array from a number of arguments:

Example

Example Code

javascript
Try it Yourself
const myArr = Int16Array.of(1,2,3,4,5,6,7,8,9,0);

The constructor.name Property

The constructor.name property returns the name (type) of a typed array:

Example

Example Code

javascript
Try it Yourself
myArr.constructor.name

The BYTES_PER_ELEMENT Property

BYTES_PER_ELEMENT returns the number of bytes used to store each array element:

Example

Example Code

javascript
Try it Yourself
myArr.BYTES_PER_ELEMENT

Common Array Methods

Typed Arrays share many methods with Standard Arrays:

  • Iteration: forEach(), map(), filter(), reduce(), reduceRight(), every(), some(), find(), findIndex(), findLast(), findLastIndex().
  • Searching: includes(), indexOf(), lastIndexOf().
  • Manipulation: at(), copyWithin(), fill(), reverse(), set(), slice(), sort(), subarray().
  • Conversion: join(), toLocaleString(), toString().
  • Non-mutating methods: toReversed(), toSorted(), with().

The fill() Method

The fill() method changes all elements in a typed array to a value:

Example

Example Code

javascript
Try it Yourself
myArr.fill(200);

The fill() method takes two optional arguments: start index and end index:

Example

Example Code

javascript
Try it Yourself
myArr.fill(200, 0, 3);

The find() Method

The find() method returns the first element that satisfies a test:

Example

Example Code

javascript
Try it Yourself
myArr.find((x) => x > 18)

The some() Method

The some() method returns true if an element for which a provided function returns true:

Example

Example Code

javascript
Try it Yourself
myArr.some((x) => x > 18)

Not Available Array Methods

Some array methods are NOT available for typed array.

This is due to the fixed-length nature and the lack of fixed structure.

MethodArrayTyped Array
pop()YesNO
push()YesNO
shift()YesNO
unshift()YesNO
splice()YesNO
flat()YesNO
flatMap()YesNO
concat()YesNO
toSpliced()YesNO

Browser APIs Supporting Typed Arrays

Fetch API Example

Example Code

javascript
Try it Yourself
fetch(url)

.then(request => request.arrayBuffer())

.then(arrayBuffer =>...);

Canvas Example

Example Code

javascript
Try it Yourself
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;

Learn More: > Typed Array Tutorial > Typed Array Reference > Array Buffers > DataViews > Atomics

Browser Support

Typed Arrays is an ES6 feature.

ES6 is fully supported in all modern browsers since June 2017:

Chrome 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016

Interactive Code Examples

Examples

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const myArr = Int16Array.from("1234567890");

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const myArr = Int16Array.of(1,2,3,4,5,6,7,8,9,0);

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
myArr.constructor.name

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
myArr.BYTES_PER_ELEMENT

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
myArr.fill(200);

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
myArr.fill(200, 0, 3);