Open In App

How Dynamic Arrays Work in JavaScript ?

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A dynamic array, also known as a resizable array or a dynamic array list, is a data structure that allows its size to be changed dynamically during program execution. Unlike static arrays, which have a fixed size determined at the time of declaration, dynamic arrays can grow or shrink in size as needed. In JavaScript, dynamic arrays are typically implemented using the built-in Array object. Unlike some other languages, JavaScript arrays are not fixed in size, and you can dynamically add or remove elements as needed.

Key Points of Dynamic Array in JavaScript:

Dynamic arrays allocate memory for elements and can dynamically resize:

  1. Memory Allocation: Initially allocates space for elements.
  2. Adding Elements: Doubles capacity if needed, copies elements, and adds the new element.
  3. Array Doubling: Doubles size to reduce frequent resizes, maintaining constant-time adds.
  4. Constant Time: The average time to add is constant, despite occasional resizing.
  5. Shrinking and Efficiency: Can shrink to free memory, with a threshold to avoid unnecessary resizing

How do dynamic arrays work in JavaScript?

Here’s a brief overview of how dynamic arrays work in JavaScript:

Declaration and Initialization:

You can create an array and initialize it with elements using square brackets:

let dynamicArray = [1, 2, 3];

Dynamic Sizing:

Arrays in JavaScript are dynamic, meaning you can easily change their size by adding or removing elements. You can use methods like push() to add elements to the end of the array:

dynamicArray.push(4); // Adds 4 to the end

Similarly, you can use methods like pop() to remove elements from the end:

dynamicArray.pop(); // Removes the last element (4)

Accessing Elements:

You can access elements in the array using their index. In JavaScript, array indices start from 0:

let elementAtIndex2 = dynamicArray[2]; // Retrieves the element at index 2 (3)

Dynamic Methods:

JavaScript arrays come with several built-in methods that allow dynamic manipulation. For instance, splice() can be used to add or remove elements at any position:

dynamicArray.splice(1, 0, 5); // Inserts 5 at index 1

Length Property:

The length property of an array dynamically adjusts as you add or remove elements:

let arrayLength = dynamicArray.length; // Returns the current length of the array

Iterating Over Elements:

You can use loops, such as for or forEach, to iterate over the elements of the array:

dynamicArray.forEach(element => {
console.log(element);
});

Example: Below is an example of dynamic array in JavaScript

Javascript




let myArray = [1, 2, 3, 4, 5];
 
// Adding elements
myArray.push(6);
myArray.push(7);
 
// Removing elements
myArray.pop();
 
// Changing length directly
myArray.length = 3;
 
// Accessing elements
console.log(myArray[2]); // Outputs 3


Output

3

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads