In JavaScript, a TypedArray is a type of object that provides a way to work with binary data in a specific format.
TypedArrays are part of the ArrayBuffer object, and they offer a structured way to deal with raw binary data.
There are several types of TypedArrays, each corresponding to a specific data type and format.
Here are some commonly used TypedArrays:
These represent arrays of 8-bit, 16-bit, 32-bit signed and unsigned integers, as well as 32-bit and 64-bit floating-point numbers.
These are for working with 64-bit signed and unsigned integers.
Ways we can create TypedArrays:
// Creating an ArrayBuffer let buffer = new ArrayBuffer(16); // Creating a TypedArray using the ArrayBuffer let int32Array = new Int32Array(buffer);
let intArray = new Int32Array([1, 2, 3, 4, 5, 6, 7, 8, 9]); let floatArray = new Float32Array(intArray);
TypedArrays provide a way to read and write binary data at a specific position.
let intArray = new Int32Array([20, 40, 60, 80, 100, 120, 140]); console.log(intArray[3]); // 80 intArray[4] = 85; console.log(intArray); // Int32Array [20, 40, 60, 80, 85, 120, 140]
TypedArrays are commonly used in scenarios where direct manipulation of binary data is required, such as working with network protocols, file I/O, or WebGL.
They provide a more efficient and reliable way to handle binary data compared to traditional JavaScript arrays.