AlgoCademy
Lesson
Code

Array Length in {lang}


We can get the length of an array using the .length property like this:

let ourArray = [50, 40, 30];

// Printing the length:
console.log(ourArray.length); // Output: 3

// Changing the array:
ourArray.pop();

// Printing the new length:
console.log(ourArray.length); // Output: 2

Using the length

We can use the length to access items from the end of an array.

Because arrays are 0-indexed, the index of the last item is length - 1:

let ourArray = [50, 40, 30];

// Printing the last item:
console.log(ourArray[ourArray.length - 1]); // Output: 30

// Printing the second to last item:
console.log(ourArray[ourArray.length - 2]); // Output: 40

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.