Back to all posts

JavaScript Indexed Collections


Javascript Index Collections are collections that have numeric indices i.e the collections of data that are ordered by an index value. In JavaScript, an array is an indexed collection. An array is an ordered set of values that has a numeric index.

We can initialize an empty array in these 2 different ways:

var arr = new Array( element0, element1, ... );   
var arr = Array( element0, element1, ... );       
var arr = [ element0, element1, ... ];  

Element Access and Modification

Elements in an array are accessed and modified using their zero-based index. You can use square brackets [] notation or the Array.prototype methods like push(), pop(), shift(), and unshift() to add or remove elements.

const myArray = [1, 2, 3];
console.log(myArray[0]); // Output: 1

myArray[1] = 5;
console.log(myArray); // Output: [1, 5, 3]

myArray.push(6);
console.log(myArray); // Output: [1, 5, 3, 6]

Array Length and Methods

Arrays have a length property that represents the number of elements in the array. JavaScript provides a rich set of built-in methods for working with arrays, including slice(), splice(), concat(), reverse(), sort(), forEach(), map(), and many more. These methods allow you to manipulate and transform array elements easily.

Array Iteration

You can iterate over array elements using various looping constructs such as for loops, forEach(), for...of, or the map() method. Each element can be accessed in turn for processing or manipulation.

Typed Arrays:

Typed arrays are a relatively recent addition to JavaScript and provide a way to work with low-level binary data efficiently. Unlike regular arrays, typed arrays have a fixed length and store elements of a specific numeric type. Let’s delve into their main characteristics:

Declaration and Initialization

Typed arrays are created using the TypedArray() the constructor or by instantiating specific typed array classes such Int8Array, Uint32Array, Float64Array , etc. Here’s an example:

const myTypedArray = new Int16Array(4); // Create an Int16Array with length 4
const anotherTypedArray = new Uint8Array([1, 2, 3]); // Initialize Uint8Array with values [1, 2, 3]

Supported Typed Array Types:

JavaScript provides several typed array types, each representing a specific data type. Some commonly used types include Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, and Float64Array. Each type has a fixed size per element, ensuring efficient memory allocation.

Element Access and Modification:

Typed arrays also use zero-based indexing for element access. Elements can be assigned or accessed using square bracket notation. Since typed arrays enforce specific numeric types, assigning values of incompatible types will be coerced or cause an error.

Working with Binary Data:

Typed arrays are particularly useful when working with binary data such as file handling, network protocols, or audio processing. They offer methods for direct manipulation of binary data, like setting and getting values from specific byte positions.

Performance Benefits:

Typed arrays offer improved performance compared to regular arrays when dealing with large amounts of binary data. Their memory layout is contiguous, allowing efficient data access and minimizing memory overhead.

Conclusion:

JavaScript provides powerful indexed collections in the form of arrays and typed arrays. Arrays are dynamic, flexible, and ideal for general-purpose collection management. On the other hand, typed arrays offer optimized performance and enable efficient manipulation of binary data. Understanding the features, similarities, and differences between these indexed collections empowers JavaScript developers to choose the most suitable data structure for their specific use cases. Whether you’re building web applications, handling large datasets, or working with binary data, a solid understanding of JavaScript indexed collections is essential to harness the full potential of the language.