JavaScript arrays come in different forms and this post will explain what the difference is between each array type. This post will look at the following array types;
- Homogeneous arrays
- Heterogeneous arrays
- Multidimensional arrays
- Jagged arrays
Homogeneous Arrays
As the name may suggest a homogeneous array is an array that stores a single data type(string, int or Boolean values).
var array = ["Matthew", "Simon", "Luke"]; var array = [27, 24, 30]; var array = [true, false, true];
Heterogeneous Arrays
A heterogeneous array is the opposite to a homogeneous array. Meaning it can store mixed data types.
var array = ["Matthew", 27, true];
Multidimensional Arrays
Also known as an array of arrays, multidimensional arrays allow you to store arrays within arrays, a kind of “array-ception”.
var array = [["Matthew", "27"], ["Simon", "24"], ["Luke", "30"]];
Jagged Arrays
Jagged arrays are similar to multidimensional array with the exception being that a jagged array does not require a uniform set of data.
var array = [ ["Matthew", "27", "Developer"], ["Simon", "24"], ["Luke"] ];
as you can see, each array within the overall array is not equal, the first array has three items stored in it, while the second has two and the third has just one.