JavaScript Array Iteration Essentials (forEach) Quiz
Cover 40 questions on how Array.forEach works, callback parameters, side effects, mutation patterns, differences versus loops and map/filter, thisArg usage, arrow callbacks, early-exit limitations, nested iterations, object handling, common patterns, debugging techniques, and best practices.
Question 1
What does Array.forEach primarily do?
Question 2
What value does forEach return?
Question 3
Which arguments does the forEach callback receive?
Question 4
Does forEach skip empty slots in sparse arrays?
Question 5
What happens if you mutate the original array while forEach is running?
Question 6
Which statement describes the difference between forEach and map?
Question 7
Is it possible to break out of a forEach loop using break?
Question 8
Which method allows early exit while iterating?
Question 9
What does the following code log?
const nums = [10, 20, 30]
nums.forEach((value, index, arr) => {
console.log(index, value, arr === nums)
})Question 10
Which pattern mutates each element in place?
const names = ['a', 'b']
names.forEach((value, idx, arr) => arr[idx] = value.toUpperCase())Question 11
How many times does this forEach run?
const list = ['x', 'y']
list.forEach(() => {
list.push('z')
})Question 12
What does the following log?
const letters = ['a','b','c']
letters.forEach((value, index) => {
if (index === 1) letters[index] = 'z'
})
console.log(letters)Question 13
Which statement about side effects is true?
Question 14
What is a common debugging pattern when using forEach?
Question 15
When should you prefer map over forEach?
Question 16
Which method is better if you want to filter elements while iterating?
Question 17
How do you supply a custom this context to forEach?
Question 18
What is the value of this inside an arrow function callback passed to forEach?
Question 19
What does the following code log?
const obj = {
prefix: 'Item',
log(value) {
console.log(this.prefix, value)
}
}
const values = [1, 2]
values.forEach(obj.log, obj)Question 20
Which pattern replicates the map behavior using forEach?
Question 21
Why might you choose a traditional for loop over forEach?
Question 22
What is a nested forEach commonly used for?
Question 23
What does this code output?
const matrix = [[1,2],[3,4]]
matrix.forEach(row => {
row.forEach(value => console.log(value))
})Question 24
Which statement describes iterating objects inside arrays?
Question 25
What is a common pattern for summing numbers using forEach?
Question 26
Which snippet logs indexes of truthy values?
const tags = ['ok', '', 'run']
tags.forEach((value, index) => {
if (value) console.log(index)
})Question 27
How do you handle asynchronous operations inside forEach?
Question 28
What does console.table help with during forEach debugging?
Question 29
Which technique logs each element with its index?
items.forEach((value, index) => console.log(`${index}: ${value}`))Question 30
Which practice keeps forEach callbacks readable?
Question 31
How do you iterate an array of objects and collect a specific field?
const names = []
users.forEach(user => names.push(user.name))Question 32
What does this pattern accomplish?
const updates = []
todos.forEach(todo => {
if (!todo.done) updates.push(todo.id)
})Question 33
How do you emulate break inside forEach?
Question 34
What does Array.from + forEach enable when iterating over array-like objects?
Question 35
Which pattern compares forEach to a for loop?
array.forEach((value, index) => {
// same as for (let i = 0; i < array.length; i++)
})Question 36
How do you avoid callback hell when using nested forEach for complex transformations?
Question 37
Which snippet demonstrates chaining the original array after forEach?
const arr = [1,2]
arr.forEach(v => console.log(v))
const doubled = arr.map(v => v * 2)Question 38
What is a best practice for handling errors inside forEach callbacks?
Question 39
Which approach helps keep forEach pure when necessary?
Question 40
What is a recommended best practice for choosing iteration helpers?
