Accessing Array Element

Intro to Arrays

Accessing Array Element

🙋 Need help? Ask an expert now!

Array elements are accessed using the indexer operator [] (square brackets). The array indexer takes the element’s index as parameter in the range from 0 to array.length-1. The first element has an index of 0. This is true for arrays, strings, and any other objects that has an index. The last element therefore has an index of array.length-1. Array elements can be retrieved and changed by the [] (indexer) operator.

Counting in an array is different from counting normally. Always remember to count from 0 when working with the index of an object. The .length is a property of the Array object that is access using the dot notation. This will print the current length of the array, even if it changes as the program runs. The indexer is used in the code above to access different elements of the array and to print them to the console.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var arr = new Array(3);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
console.log(arr[0] + "" + arr[1] + "" + arr[2]);

What will the following code segment print to the console?