Learn
Arrays
Nested Arrays
A nested array is simply an array that contains other arrays as its elements. This allows us to create a multidimensional structure, similar to a table or grid, where each element can hold its own set of values.
Creating Nested Arrays
To create a nested array, you can simply include arrays as elements within an outer array. Here's an example:
In the above code snippet, we have created a nested array called nestedArray
with three inner arrays.
Accessing Elements in Nested Arrays
Accessing elements in nested arrays requires an extra level of indexing. You can access a specific element by using square brackets []
and specifying the index of both the outer array and the inner array. Here's an example:
In the above code snippet, we accessed the element with index 1
in the outer array, and then accessed the element with index 2
in the inner array.
Modifying Elements in Nested Arrays
Modifying elements in nested arrays follows the same principle as accessing them. You can assign new values to specific elements by using the indexing syntax. Here's an example:
In the above code snippet, we modified the element at index 1
in the first inner array to have a new value of 10
.
Instructions
Create a variable called firstElement
and set it equal to the first element of the nested array.
Create a variable called lastElement
and set it equal to the last element of the nested array.
Create a variable called middleElement
and set it equal to the middle element of the nested array.
Sign up to start coding
Already have an account?
Sign In