This post will discuss about Jagged arrays in JavaScript.

1. What is Jagged arrays?

A jagged array in JavaScript is an array of arrays that can have different lengths. For example, we can have an array of three elements, where the first element is an array of three numbers, the second element is an array of two numbers, and the third element is an array of four numbers. This is how a jagged array looks like:

 
Here, we have an array of numbers where each sub-array represents a row of a matrix, but the rows can have different numbers of columns. In this example, the first sub-array has three elements, the second sub-array has two elements, and the third sub-array has four elements.

2. Uses of Jagged arrays

Jagged arrays are useful when we need to store data that does not have a fixed or regular structure, such as a table with variable number of columns, or a list of lists with different sizes. For example, we can use a jagged array to store a list of students and their grades, where each student may have a different number of grades. A jagged array may look something like this:

 
In this example, the first element of each sub-array is the name of the student, and the rest are their grades. We can use a jagged array to perform operations on the data, such as calculating the average grade for each student or finding the highest grade in the class.

3. Access elements of a jagged array

To access the elements of a jagged array, we can use the bracket notation with two indices: one for the sub-array and one for the element within the sub-array. i.e., the first index specifies the position of the subarray in the jagged array, and the second index specifies the position of the element in the subarray. Here’s an example:

Download  Run Code

 
We can also modify the elements of a jagged array by assigning new values to them using the bracket notation. For example, to change the number 7 to 8 in the jagged array above, we can use jaggedArray[2][0] = 8;.

4. Iterate over Jagged arrays

We can also use loops to iterate over the elements of a jagged array. For example, we can use a nested for loop to print all the elements in a jagged array:

Download  Run Code

Output:

1
2
3
4
5
6

5. Advantages and Disadvantages of Jagged arrays

Jagged arrays have some advantages and disadvantages compared to regular two-dimensional arrays. Some of the advantages are:

  • They can save memory space by avoiding empty cells.
  • They can represent data that is naturally irregular, such as a spreadsheet with different number of columns for each row.
  • They can be dynamically resized by adding or removing elements from the nested arrays.

 
Some of the disadvantages are:

  • They can be harder to iterate over, since we need to check the length of each row before accessing its elements.
  • They can be slower to access, since we need to traverse two levels of arrays instead of one.
  • They can be less readable and maintainable, since they require more code and logic to manipulate.

That’s all about Jagged arrays in JavaScript.