Video Lesson

Lesson Notes

Accessing Array Elements by Index in JavaScript

Arrays in JavaScript are zero-indexed, meaning the first element is at position 0.

Example Array

1const names = ["John", "Mary", "Susie", "Jay", "Edward", "Mike"]; 2console.log(`All names: ${names}`); 3// Outputs: All names: John,Mary,Susie,Jay,Edward,Mike

Getting the Length

1console.log(`Length of array: ${names.length}`); 2// Outputs: Length of array: 6

Accessing by Index

  • Index 0: First element
  • Index 1: Second element
  • Index 5: Sixth element (last, since length is 6)
1console.log(names[0]); // "John" 2console.log(names[4]); // "Edward" 3console.log(names[5]); // "Mike"

Key Points

  • Indexes start at 0, so the last element is at length - 1.
  • Use array[index] to retrieve or modify elements.

Why It Matters

Looping over arrays often uses indexes:

1for (let i = 0; i < names.length; i++) { 2 console.log(`Name at index ${i}: ${names[i]}`); 3}

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Array Indexing, Access Elements

In this lesson, students will learn how to access and manipulate array elements in JavaScript using their indices, understanding the concept of zero-based indexing. By mastering these skills, learners will be equipped to efficiently work with arrays and enhance their programming capabilities.

2m 32s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed