Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 6, Topic 11
In Progress

JavaScript: Nested Arrays

Yasin Cakal 16 Nov 2021
Lesson Progress
0% Complete

Arrays, as previously stated, can store other arrays. A nested array is an array that contains another array. Consider the following example:

const nestedArr = [[1,2], [3,4]];

We can use bracket notation with the index value to access the nested arrays in the same way we did for any other element:

const nestedArr = [[1,2], [3,4]];
console.log(nestedArr[0]); 

// Output: [1, 2]

It’s worth noting that nestedArr[0] will select the element at index 0, which is the array [1, 2]. We can then chain, or add on, more bracket notation with index values to access the elements within the nested array.

const nestedArr = [[1,2], [3,4]];
console.log(nestedArr[0]); 
console.log(nestedArr[1][0]);

// Output: [ 1, 2 ]
//          3

Two bracket notations are chained to nestedArr in the second console.log() statement. We already know that nestedArr[1] represents the array [3, 4]. Then, to get the first element of that array, we use nestedArr[1][0], which returns the value 3.

Responses

Leave a Reply

%d bloggers like this: