We use arrays to store lists of items. Accessing array elements means getting a specific item from the list by its position.
Accessing array elements in Javascript
const arrayName = [item1, item2, item3]; const element = arrayName[index];
Array positions start at 0, so the first element is at index 0.
If you try to access an index outside the array, you get undefined.
const fruits = ['apple', 'banana', 'cherry']; const firstFruit = fruits[0]; // 'apple'
length - 1.const numbers = [10, 20, 30]; const lastNumber = numbers[numbers.length - 1]; // 30
undefined.const emptyArray = []; const element = emptyArray[0]; // undefined
const colors = ['red']; const onlyColor = colors[0]; // 'red'
This program creates an array of animals and accesses elements at different positions, including an index that does not exist.
const animals = ['dog', 'cat', 'rabbit']; console.log('Animals array:', animals); const firstAnimal = animals[0]; console.log('First animal:', firstAnimal); const secondAnimal = animals[1]; console.log('Second animal:', secondAnimal); const lastAnimal = animals[animals.length - 1]; console.log('Last animal:', lastAnimal); const outOfBounds = animals[5]; console.log('Out of bounds access:', outOfBounds);
Accessing an element by index is very fast, with time complexity O(1).
Trying to access an index outside the array returns undefined, which can cause bugs if not checked.
Use array length to find the last element's index, since arrays can change size.
Arrays store items in order, starting at index 0.
You get an element by using its index inside square brackets.
Accessing an invalid index returns undefined.
Practice
Solution
Step 1: Understand array indexing
JavaScript arrays start counting elements from 0, not 1.Step 2: Identify the first element's index
The first element is always at index 0.Final Answer:
0 -> Option CQuick Check:
First element index = 0 [OK]
- Thinking first element index is 1
- Confusing negative indexes
- Assuming index depends on array length
colors?Solution
Step 1: Recall array access syntax
JavaScript arrays use square brackets with zero-based index to access elements.Step 2: Calculate index for third element
The third element is at index 2 (0,1,2).Final Answer:
colors[2] -> Option BQuick Check:
Third element index = 2 [OK]
- Using parentheses instead of brackets
- Using curly braces
- Using index 3 for third element
const fruits = ['apple', 'banana', 'cherry']; console.log(fruits[1]);
Solution
Step 1: Identify the array elements and indexes
The array fruits has 'apple' at index 0, 'banana' at index 1, and 'cherry' at index 2.Step 2: Access element at index 1
fruits[1] returns 'banana'.Final Answer:
banana -> Option DQuick Check:
fruits[1] = banana [OK]
- Choosing first element instead
- Confusing index 1 with 2
- Expecting undefined for valid index
const numbers = [10, 20, 30]; console.log(numbers(1));
Solution
Step 1: Check array access syntax
Arrays must be accessed with square brackets, not parentheses.Step 2: Identify the error in the code
numbers(1) is invalid syntax; correct is numbers[1].Final Answer:
Using parentheses instead of square brackets -> Option AQuick Check:
Array access needs [] not () [OK]
- Using () instead of []
- Thinking index 1 is invalid
- Ignoring syntax errors
const data = [5, 10, 15];, what will console.log(data[3]); output?Solution
Step 1: Understand array length and valid indexes
The array has elements at indexes 0, 1, and 2 only.Step 2: Access index 3 which does not exist
Accessing an index outside the array returns undefined in JavaScript.Final Answer:
undefined -> Option AQuick Check:
Out-of-range index returns undefined [OK]
- Expecting error instead of undefined
- Confusing last element with index 3
- Assuming 0 is default value
Ready to prove it?
Pass the test and this pattern moves unknown
