JavaScript

Is There an ArrayList in JavaScript?

In programming languages, an “ArrayList” is a data structure that stores a list of values, similar to an array. ArrayList is similar/identical to an array, but its size can be modified dynamically. It allows the addition, removal, and manipulation of elements at a specific position within the array. An ArrayList is similar to an array, but it has some additional methods to add, remove, and search elements more efficiently.

This article will describe the ArrayList in JavaScript.

Is There an ArrayList in JavaScript?

In JavaScript, there is no pre-built data type called “ArrayList”. However, JavaScript does have an “array” data type, which allows you to store a collection of values in a single variable. More specifically, arrays in JavaScript are dynamic, which means that you can add or remove elements from the array at any time.

Let’s see an example of using an array as an ArrayList by dynamically adding and deleting elements.

Example 1: Add Elements in ArrayList

Create an array named “ArrayList” that stores numbers and strings:

var arrayList = [11, 15, "USA", "JavaScript", "false" ];

Now, add a boolean value “true” in an array using the array’s predefined method “push()”:

arrayList.push(true);

Add one more element like “23” in an array:

arrayList.push(23);

Print the array on the console:

console.log(arrayList);

The output indicates that the elements have been successfully added to an array:

Now, let’s delete some elements from an array like an ArrayList.

Example 2: Delete Last Element From an ArrayList

For removing the last element from an array, use the “pop()” method:

arrayList.pop(23);

Here, you can see that the last element of an array that is “23” has been successfully removed:

Example 3: Delete First Element From an ArrayList

If you want to remove/delete the first element from an array, use the “shift()” method:

arrayList.shift(11);

Output

Example 4: Replace Element From an ArrayList

Sometimes, we want to replace any element with another element or add an element at any specified index in an array. So, utilize the “splice()” method in that situation. Here, we will add an element at the index 1 and without deleting any element:

arrayList.splice(1, 0, "Linuxhint");

It can be seen that the “Linuxhint” is added at the 1st index of an array:

We have compiled all the necessary information relevant to the ArrayList in JavaScript.

Conclusion

In JavaScript, there is no built-in data type called “ArrayList” like in some other programming languages. To create a dynamic list or an “ArrayList” in JavaScript, use an “array” and the built-in array methods to add and remove elements as needed. This article described the arrayList in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.