Basic Data Structures - Access an Array's Contents Using Bracket Notation

Hi,

I cannot comprehend what this line in theory means. Can somebody explain a little please.

" This index doubles as the position of that item in the array, and how you reference it."

Challenge: Basic Data Structures - Access an Array’s Contents Using Bracket Notation

Link to the challenge:

In javaScript, array is a collection of elements (can be a number, string, boolean, etc) which are surrounded by square brackets. And the we can access (or get the value of any element) using the bracket notation. But first we need to understand how elements are numbered in the array. For example,

let myArray = ["Apple",  42, "Orange", true, 90];

In the above example myArray element “Apple” has position number 0, 42 has position number 1, “Orange” has position number 3 etc. That means array has zero-based indexing for elements.
And if we need to find the value of the 3rd element in myArray that is the value of the element at index 2. We can achieve it by the following code that is –

let item = myArray[2];

The variable item would have a value of “Orange”. Similarly we can modify the value of 3rd element (element at index 2) with any other value like “Pineapple”.
The code below can achieve it

myArray[2] = "Pineapple";

Now my Array will contain

myArray = ["Apple",  42, "Pineapple", true, 90];

Similarly, you need to modify the letter “b” of myArray to something else.

" This index doubles as the position of that item in the array, and how you reference it." - This line means the index also acts as a way to reference the position of the element in the array.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.