Example 1 — Double Every Element
An Int16Array of prices in cents is doubled. The new typed array has the same type and the same length.
const cents = new Int16Array([150, -20, 300]);
const doubled = cents.map((v) => v * 2);
console.log(doubled.join(' ')); // 300 -40 600
console.log(doubled instanceof Int16Array); // true
console.log(doubled.length + ' of ' + cents.length); // 3 of 3
console.log(cents.join(' ')); // 150 -20 300 How It Works
- One result per element. The callback runs for 150, -20, and 300, and each returned number goes into the same position of the new typed array.
- Same type, same length. The result is another
Int16Arraywith three elements, so negative numbers still fit. - The source stays as it was. The last line proves that
centsstill holds 150, -20, and 300;map()works on a copy.

