J

JavaScript Handbook

Clean • Professional

JavaScript Typed Array Methods

3 minute

JavaScript Typed Array Methods

JavaScript typed arrays support many of the standard array methods, but with some limitations because they are fixed-length arrays and store only specific numeric types. Here's a structured overview:

copyWithin(target, start, end)

Copies a sequence of elements within the same typed array to another position. Overwrites existing values but does not change array length.

const arr = new Int8Array([10, 20, 30, 40]);
arr.copyWithin(1, 2);
console.log(arr); // [10, 30, 40, 40]

entries()

Returns an iterator of [index, value] pairs, useful for looping over indices and values simultaneously.

const arr = new Uint8Array([5, 10]);
for (let [i, v] of arr.entries()) {
    console.log(i, v);
}
// 0 5
// 1 10

every(callback)

Checks if all elements satisfy a test. Returns true if all pass, otherwise false.

const arr = new Int16Array([2, 4, 6]);
console.log(arr.every(x => x % 2 === 0)); // true

fill(value, start, end)

Fills elements with a static value. start is inclusive, end is exclusive.

const arr = new Uint8Array(5);
arr.fill(1);
console.log(arr); // [1, 1, 1, 1, 1]

filter(callback)

Creates a new typed array containing only elements that satisfy the provided condition.

const arr = new Int8Array([1, 2, 3, 4]);
const filtered = arr.filter(x => x > 2);
console.log(filtered); // [3, 4]

find(callback)

Returns the first element that satisfies a condition. If none found, returns undefined.

const arr = new Uint16Array([10, 20, 30]);
console.log(arr.find(x => x > 15)); // 20

findIndex(callback)

Returns the index of the first element that satisfies a condition. Returns -1 if none found.

const arr = new Int32Array([5, 10, 15]);
console.log(arr.findIndex(x => x === 10)); // 1

forEach(callback)

Executes a function for each element. Does not return a new array.

const arr = new Float32Array([1.1, 2.2]);
arr.forEach(x => console.log(x));
// 1.1
// 2.2

indexOf(value)

Returns the first index of a value, or -1 if the value is not found.

const arr = new Int8Array([10, 20, 30]);
console.log(arr.indexOf(20)); // 1

join(separator)

Converts the typed array into a string, joining elements with a specified separator.

const arr = new Uint8Array([1, 2, 3]);
console.log(arr.join('-')); // "1-2-3"

keys()

Returns an iterator of indices for the typed array. Useful for looping over positions.

const arr = new Int16Array([7, 8]);
for (let k of arr.keys()) console.log(k);
// 0
// 1

lastIndexOf(value)

Returns the last occurrence index of a value.

const arr = new Int8Array([10, 20, 10]);
console.log(arr.lastIndexOf(10)); // 2

map(callback)

Creates a new typed array with each element transformed according to the callback function.

const arr = new Float32Array([1, 2, 3]);
const squared = arr.map(x => x * x);
console.log(squared); // [1, 4, 9]

reduce(callback, initialValue)

Reduces array elements to a single value, processing left-to-right.

const arr = new Uint8Array([1, 2, 3]);
console.log(arr.reduce((sum, x) => sum + x, 0)); // 6

reduceRight(callback, initialValue)

Reduces array elements right-to-left to a single value.

const arr = new Int8Array([1, 2, 3]);
console.log(arr.reduceRight((sum, x) => sum + x, 0)); // 6

reverse()

Reverses the typed array in place.

const arr = new Uint16Array([1, 2, 3]);
arr.reverse();
console.log(arr); // [3, 2, 1]

set(array, offset)

Copies values from another array into the typed array starting at a specific offset.

const arr = new Int8Array(4);
arr.set([1, 2], 1);
console.log(arr); // [0, 1, 2, 0]

slice(start, end)

Returns a new typed array with a portion of the original array. Does not modify the original.

const arr = new Uint8Array([10, 20, 30, 40]);
const part = arr.slice(1, 3);
console.log(part); // [20, 30]

some(callback)

Returns true if at least one element passes the test; otherwise false.

const arr = new Int8Array([1, 2, 3]);
console.log(arr.some(x => x > 2)); // true

subarray(start, end)

Returns a new view of the typed array without copying data.

const arr = new Int16Array([10, 20, 30, 40]);
const sub = arr.subarray(1, 3);
console.log(sub); // [20, 30]

toLocaleString()

Returns a string representing elements with locale-sensitive formatting.

const arr = new Float32Array([1234.56, 7890.12]);
console.log(arr.toLocaleString()); // "1,234.56,7,890.12"

toString()

Converts the typed array to a comma-separated string.

const arr = new Uint8Array([1, 2, 3]);
console.log(arr.toString()); // "1,2,3"

values()

Returns an iterator over the values of the typed array.

const arr = new Int8Array([5, 10]);
for (let v of arr.values()) console.log(v);
// 5
// 10

Typed Array Methods Reference

MethodDescription
copyWithin(target, start, end)Copies part of the array to another position in the same array, overwriting existing elements.
entries()Returns an iterator of [index, value] pairs.
every(callback)Checks if all elements pass a test. Returns true or false.
fill(value, start, end)Fills array elements with a static value. Optional start and end indices.
filter(callback)Returns a new typed array with elements passing a test.
find(callback)Returns the first element that satisfies a condition.
findIndex(callback)Returns the index of the first element satisfying a condition.
forEach(callback)Executes a function for each element.
indexOf(value)Returns the first index of a value or -1 if not found.
join(separator)Converts array into a string with a specified separator.
keys()Returns an iterator of the array’s indices.
lastIndexOf(value)Returns the last index of a value.
map(callback)Returns a new typed array with transformed elements.
reduce(callback, initialValue)Reduces array to a single value from left to right.
reduceRight(callback, initialValue)Reduces array to a single value from right to left.
reverse()Reverses elements in place.
set(array, offset)Copies values from another array into the typed array starting at a specific offset.
slice(start, end)Returns a new typed array containing a portion of the array.
some(callback)Returns true if at least one element satisfies a test.
subarray(start, end)Returns a new view of the array without copying data.
toLocaleString()Returns a string representing elements with locale-sensitive formatting.
toString()Converts array to a comma-separated string.
values()Returns an iterator over the array values.


Article 0 of 0