Multi-dimensional Arrays

Discussion on multi-dimensional arrays in JavaScript.

We know how JavaScript allows us to make all types of arrays. More importantly, JavaScript gives the flexibility to create an array of values of any type. This increases the flexibility of array object uses.

This lesson will go over how arrays let us append elements to create multi-dimensional arrays.

Introduction to multi-dimensional arrays

Earlier, we saw single-dimensional arrays containing elements of various or same types.

The below code shows a single-dimension array.

Press + to interact
var arr = [1, 2, 3, 4]; // Signle-dimensional array
console.log("arr:", arr, "arr length:",arr.length);

That code consists of an array of four elements. Each element is a number. What happens if we push this array into an empty array?

Press + to interact
var new_arr = []
var arr = [1, 2, 3, 4]; // Single-dimensional array
console.log("arr:", arr, "arr length:",arr.length);
new_arr.push(arr); // push arr into new_arr
console.log("new_arr:",new_arr, "new_arr length:",new_arr.length);

Here, we have an array of ...