Array traversal means visiting each element in an array one by one to perform some operation or check.
Click to reveal answer
beginner
Name two common ways to traverse an array in JavaScript.
Using a for loop and using the forEach() method are two common ways to traverse an array.
Click to reveal answer
intermediate
What does 'traversing an array backwards' mean?
It means visiting elements starting from the last element to the first, usually by looping from the highest index down to zero.
Click to reveal answer
intermediate
Why might you use a while loop instead of a for loop for array traversal?
A while loop is useful when you don't know how many elements you will process in advance or want to stop early based on a condition.
Click to reveal answer
intermediate
Explain the difference between forEach() and map() when traversing arrays.
forEach() runs a function on each element but does not return a new array. map() runs a function on each element and returns a new array with the results.
Click to reveal answer
Which loop is best when you want to traverse an array from end to start?
Awhile loop with index increasing
BforEach() method
Cmap() method
Dfor loop with index decreasing
✗ Incorrect
A for loop with the index decreasing from the last element to the first is best for backward traversal.
What does the forEach() method do?
ACreates a new array with transformed elements
BStops traversal when a condition is met
CExecutes a function on each array element without returning a new array
DTraverses the array backwards
✗ Incorrect
forEach() runs a function on each element but does not return a new array.
Which of these is NOT a valid way to traverse an array in JavaScript?
Aif statement
Bfor loop
Cdo-while loop
Dwhile loop
✗ Incorrect
An if statement alone does not traverse an array; loops are needed to visit multiple elements.
What is the starting index when traversing an array forwards?
A0
B1
Carray length
Darray length - 1
✗ Incorrect
Array indexes start at 0, so forward traversal starts at index 0.
Which method returns a new array after traversal?
AforEach()
Bmap()
Cfilter()
Dreduce()
✗ Incorrect
map() returns a new array with the results of applying a function to each element.
Describe three different ways to traverse an array in JavaScript and when you might use each.
Think about loops and array methods.
You got /3 concepts.
Explain how to traverse an array backwards and why it might be useful.
Consider index counting and use cases.
You got /2 concepts.
Practice
(1/5)
1. What does the following code do? for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
easy
A. Prints only the first element of the array
B. Prints the array elements in reverse order
C. Prints each element of the array from first to last
D. Prints the length of the array
Solution
Step 1: Understand the loop range
The loop starts at 0 and runs while i is less than arr.length, so it covers all valid indexes.
Step 2: Check the action inside the loop
Each iteration prints arr[i], which is the element at index i, so all elements are printed in order.
Final Answer:
Prints each element of the array from first to last -> Option C
Quick Check:
Loop from 0 to length prints all elements [OK]
Hint: Loop from 0 to length prints all elements in order [OK]
Common Mistakes:
Thinking it prints in reverse order
Assuming it prints only one element
Confusing index with value
2. Which of the following is the correct syntax to traverse an array arr using a for...of loop in JavaScript?
easy
A. for (let element of arr) { console.log(element); }
B. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
C. for (let i in arr) { console.log(arr[i]); }
D. for (let i = 0; i <= arr.length; i++) { console.log(arr[i]); }
Solution
Step 1: Identify the for...of syntax
The for...of loop iterates over values directly, so syntax is for (let element of arr).
Step 2: Check each option
for (let element of arr) { console.log(element); } matches the correct for...of syntax. Others use different loops or incorrect conditions.
Final Answer:
for (let element of arr) { console.log(element); } -> Option A
Quick Check:
for...of loops over values [OK]
Hint: Use 'for (let x of arr)' to loop over values directly [OK]
Common Mistakes:
Using 'for...in' instead of 'for...of'
Using <= instead of < in loop condition
Confusing index and element in loops
3. What is the output of the following code?
const arr = [10, 20, 30];
let result = '';
for (let i = arr.length - 1; i >= 0; i--) {
result += arr[i] + ' ';
}
console.log(result.trim());
medium
A. 10 20 30
B. 30 20 10
C. SyntaxError
D. 30 10 20
Solution
Step 1: Understand the loop direction
The loop starts at index 2 (arr.length - 1) and decrements to 0, so it goes backward through the array.
Step 2: Trace the concatenation
Elements are added in order: arr[2] = 30, arr[1] = 20, arr[0] = 10, separated by spaces.
Final Answer:
30 20 10 -> Option B
Quick Check:
Loop backward prints reversed array [OK]
Hint: Loop from length-1 down to 0 prints array in reverse [OK]
Common Mistakes:
Confusing loop direction and printing original order
Off-by-one errors in loop condition
Forgetting to trim trailing space
4. Find the error in this code snippet that tries to print all elements of arr:
const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
medium
A. Using <= causes accessing undefined element at the end
B. Missing declaration of i
C. Should use for...of loop instead
D. Array length is incorrect
Solution
Step 1: Analyze loop condition
The loop runs while i <= arr.length, so i reaches arr.length which is out of bounds.
Step 2: Understand effect of out-of-bounds access
Accessing arr[arr.length] returns undefined, so console.log prints undefined after last element.
Final Answer:
Using <= causes accessing undefined element at the end -> Option A
Quick Check:
Loop should use < not <= for array indexes [OK]
Hint: Use i < arr.length, not <=, to avoid undefined access [OK]
Common Mistakes:
Using <= instead of < in loop condition
Ignoring undefined output at end
Thinking array length is zero-based
5. Given a nested array arr = [[1, 2], [3, 4], [5, 6]], which code correctly prints all elements in order using nested loops?
hard
A. for (let i in arr) { for (let j in arr) { console.log(arr[i][j]); } }
B. for (let i of arr) { console.log(i); }
C. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
D. for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } }
Solution
Step 1: Understand nested array structure
arr is an array of arrays, so to access each element, two loops are needed: outer for sub-arrays, inner for elements.
Step 2: Check each option
for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } } uses two loops with correct ranges and indexes to print each element. for (let i of arr) { console.log(i); } prints sub-arrays, not elements. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } prints sub-arrays. for (let i in arr) { for (let j in arr) { console.log(arr[i][j]); } } incorrectly loops inner array length.
Final Answer:
for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } } -> Option D
Quick Check:
Nested loops needed for nested arrays [OK]
Hint: Use nested loops: outer for arrays, inner for elements [OK]
Common Mistakes:
Using single loop prints sub-arrays, not elements
Using wrong inner loop length
Confusing for...in with for...of in nested loops
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.