JavaScript TypedArray.prototype.values() Method

TypedArray’s values() method returns a new iterator object for the array, this iterator will allow you to traverse through typed arrays and generate the value of each element at every position and like values() method for normal JavaScript arrays, it serves a similar role.

Syntax

typedArray.values()

Parameter:

Does not accept any parameter.

Return Value:

An array iterator object containing the values of each element in the typed array.

Example 1: In the given below example we are iterating over values in Float32Array using value() method.

JavaScript
// Example usage of values() to
// iterate over a Float32Array
const float32Array = new Float32Array([0.1, 2.5, 3.7, 4.9, 5.5]);

const iterator = float32Array.values();

const valuesArray = [];
for (let value of iterator) {
    valuesArray.push(value);
}

console.log(valuesArray);

Output
[ 0.10000000149011612, 2.5, 3.700000047683716, 4.900000095367432, 5.5 ]

Example 2: In the given below example we are iterating over values in Unit8Array.

JavaScript
// Example usage of values() to iterate over a Uint8Array
const uint8Array = new Uint8Array([10, 20, 30, 40, 50]);

const iterator = uint8Array.values();

for (let value of iterator) {
    console.log(value);
}

Output
10
20
30
40
50

Contact Us