The filter() method creates a new typed array with all elements that pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.filter(). TypedArray is one of the typed array types here.


<var>typedarray</var>.filter(<var>callback</var>[, <var>thisArg</var>])


Function to test each element of the typed array. Invoked with arguments (element, index, typedarray). Return true to keep the element, false otherwise.
thisArg Optional
Value to use as this when executing callback.

Return value

A new typed array with the elements that pass the test.


The filter() method calls a provided callback function once for each element in a typed array, and constructs a new typed array of all the values for which callback returns a true value. callback is invoked only for indexes of the typed  array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Typed array elements which do not pass the callback test are simply skipped, and are not included in the new typed array.

callback is invoked with three arguments:

  1. the value of the element
  2. the index of the element
  3. the typed array object being traversed

If a thisArg parameter is provided to filter, it will be passed to callback when invoked, for use as its this value.  Otherwise, the value undefined will be passed for use as its this value. The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function.

filter() does not mutate the typed array on which it is called.

The range of elements processed by filter() is set before the first invocation of callback. Elements which are appended to the typed array after the call to filter() begins will not be visited by callback. If existing elements of the typed array are changed, or deleted, their value as passed to callback will be the value at the time filter() visits them; elements that are deleted are not visited.


Filtering out all small values

The following example uses filter() to create a filtered typed array that has all elements with values less than 10 removed.

function isBigEnough(element, index, array) {
  return element >= 10;
new Uint8Array([12, 5, 8, 130, 44]).filter(isBigEnough); 
// Uint8Array [ 12, 130, 44 ]

Filtering typed array elements using arrow functions

Arrow functions provide a shorter syntax for the same test.

new Uint8Array([12, 5, 8, 130, 44]).filter(elem => elem >= 10); 
// Uint8Array [ 12, 130, 44 ]


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'TypedArray.prototype.filter' in that specification.
Standard Initial definition.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'TypedArray.prototype.filter' in that specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 45 38 (38) No support No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support 38.0 (38) No support No support No support

See also


© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.

ECMAScript6 JavaScript Method Prototype TypedArray TypedArrays