Multi-dimensional arrays in JavaScript

by danielkwood

HTML

<html>
    <head>
        <title>Multi-dimensional arrays</title>
        <script src="script.js"></script>
    </head>
        
    <body>

    </body>
</html>

JavaScript

// This creates a two-dimension array
// Here there are three arrays within the people array
var people = [
    ["Joe", 16, "Sydney"], // 0
    ["Alice", 20, "Melbourne"], // 1
    ["Jack", 15, "Brisbane"], // 2
];

// This will display the second element from the second array with the people array (Alice's age)
console.log(people[1][1]);

// This will update the second element in the third array in the people array (Jack's age)
people[2][1] = 16;

// This will display the third array in the people array (all information about Jack)
console.log(people[2]);

// This will add a fourth array to the people array
people[3] = ["Sally", 30, "Perth"];

// This will display all elements from the people array
console.log(people);