Course
TypedArray
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
TypedArray Object
What is a TypedArray?
A JavaScript TypedArray is an array-like object used to store binary data. Unlike the array, the size of the TypedArray can't be dynamic and can't hold the values of the different data types, improving the performance of the TypedArray.
A TypedArray is used in audio processing, graphics rendering, networking, etc.
Why TypedArray?
In other programming languages like C++, an array can contain data of only one data type, but a JavaScript array is a bit different. It can contain elements of multiple data types. So, JavaScript arrays are less efficient in dealing with binary data and when higher performance is needed.
It is one of the reasons that TypedArray is introduced in JavaScript, and it is also called the array buffer. A TypedArray is the best way to handle binary data while maintaining the memory.
TypedArray Objects
Here are the types of TypedArray objects available to store the data of the 8, 16, 32, or 64-bit data. You can choose any object to create a TypedArray according to the data you need to store.
A TypedArray doesn't support the methods like push(), pop, etc., but supported properties and methods are listed below.
TypedArray Properties
Here is a list of the properties of the TypedArray object along with their description
TypedArray Static Methods
Here is a list of the static methods of the TypedArray object along with their description
TypedArray Instance Methods
Here is a list of the instance methods of the TypedArray object along with their description
Examples
Example 1
We used the Int8Array to create a TypedArray in the example below. We passed the array containing the multiple elements as an object constructor.
In the output, you can see that if any input element is greater than the 8-bit number, the constructor function automatically enforces it to the 8-bit number.
<html><body> <p id = "output"> </p> <script> const array = new Int8Array([1000, 200, 30, 40]); document.getElementById("output").innerHTML = "The array is: " + array; </script></body></html>
Output
The array is: -24,-56,30,40
Example 2
In the example below, we used the Float32Array() constructor function to create a TypedArray. It is used to store the 32-bit floating point numbers.
Also, you can access or update TypedArray elements as the normal array.
<html><body> <p id = "output"> </p> <script> const array = new Float32Array([100.212, 907.54, 90, 14562547356342.3454]); array[2] = 23.65; // Updating the 3rd element of the array document.getElementById("output").innerHTML = "The array is: " + array; </script></body></html>
Output
The array is: 100.21199798583984,907.5399780273438,23.649999618530273,14562546941952