UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ TypedArray/ TypedArray.prototype.find()

The find() method of TypedArray instances returns the first element in the provided typed array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. This method has the same algorithm as Array.prototype.find.

Syntax

find(callbackFn)
find(callbackFn, thisArg)

Parameters

Return value

The first element in the typed array that satisfies the provided testing function. Otherwise, undefined is returned.

Description

See Array.prototype.find for more details. This method is not generic and can only be called on typed array instances.

Examples

Find a prime number in a typed array

The following example finds an element in the typed array that is a prime number (or returns undefined if there is no prime number).

function isPrime(element, index, array) {
  let start = 2;
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}

const uint8 = new Uint8Array([4, 5, 8, 12]);
console.log(uint8.find(isPrime)); // 5

Specifications

Browser compatibility

See also