2D Arrays Basics
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 6 of 65.
A 2D array is an array containing sub-arrays. Each sub-array can be seen as a row in a grid.For example:
const cinema = [
[true, true], // Row 0
[false, true] // Row 1
];This array represents a 2x2 grid (2 items in each row, and there are 2 rows).To create a 2D array of integers with 3 rows and 4 columns, you would write:const matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
];
// 3x4 gridNote that in JavaScript, 2D arrays are flexible: sub-arrays do not need to contain the same data type, and they do not even need to be the same length.For example, you can mix types and lengths within the same 2D array:const mixed = [
[1, 2, 3],
["apple", "banana"],
[true]
];Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys