JavaScript Array Iteration Essentials (forEach) Quiz

JavaScript
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What does Array.forEach primarily do?

A
Iterates over each element and executes a callback for side effects
B
Returns a new array with transformed values
C
Filters out elements based on a predicate
D
Sorts the array in place
2

Question 2

What value does forEach return?

A
undefined
B
The original array
C
A new array of results
D
The count of iterations
3

Question 3

Which arguments does the forEach callback receive?

A
(currentValue, index, array)
B
(array, index, currentValue)
C
(currentValue, array)
D
(index, currentValue)
4

Question 4

Does forEach skip empty slots in sparse arrays?

A
Yes, forEach does not call the callback for missing indices
B
No, it fills them with undefined
C
It throws an error
D
It converts the array to dense first
5

Question 5

What happens if you mutate the original array while forEach is running?

A
Mutations are visible to subsequent iterations
B
forEach clones the array first
C
Mutations are ignored until the next run
D
A TypeError is thrown
6

Question 6

Which statement describes the difference between forEach and map?

A
map returns a new array with transformed values; forEach does not
B
They are identical
C
forEach handles asynchronous callbacks automatically
D
map modifies the original array by default
7

Question 7

Is it possible to break out of a forEach loop using break?

A
No, forEach has no break/continue support
B
Yes, break works like in for loops
C
Yes, but only with arrow functions
D
Yes, by returning false
8

Question 8

Which method allows early exit while iterating?

A
Array.some or Array.every
B
Array.forEach
C
Array.forEach with return false
D
Array.forEach with break
9

Question 9

What does the following code log?

javascript
const nums = [10, 20, 30]
nums.forEach((value, index, arr) => {
  console.log(index, value, arr === nums)
})
A
Each index-value pair plus true indicating the same array
B
Only values
C
Only indexes
D
Nothing; forEach is void
10

Question 10

Which pattern mutates each element in place?

javascript
const names = ['a', 'b']
names.forEach((value, idx, arr) => arr[idx] = value.toUpperCase())
A
names becomes ['A', 'B']
B
A new array is created
C
forEach throws an error
D
names is unchanged
11

Question 11

How many times does this forEach run?

javascript
const list = ['x', 'y']
list.forEach(() => {
  list.push('z')
})
A
Two times; new pushes are ignored because length is evaluated at start
B
Infinite loop
C
Three times
D
Zero times
12

Question 12

What does the following log?

javascript
const letters = ['a','b','c']
letters.forEach((value, index) => {
  if (index === 1) letters[index] = 'z'
})
console.log(letters)
A
['a','z','c']
B
['a','b','c']
C
['z','b','c']
D
Throws error
13

Question 13

Which statement about side effects is true?

A
forEach is appropriate when you want to modify external state or the original array
B
forEach should never modify anything
C
forEach automatically produces pure functions
D
forEach clones external references
14

Question 14

What is a common debugging pattern when using forEach?

A
Insert console.log statements inside the callback to track values and indexes
B
Use alert for each item
C
Wrap forEach in try/catch always
D
Inspect arr.length after each iteration
15

Question 15

When should you prefer map over forEach?

A
When you need a transformed array as output
B
When you only need side effects
C
When you want to mutate each element
D
When you need to early exit
16

Question 16

Which method is better if you want to filter elements while iterating?

A
Array.filter
B
Array.forEach
C
Array.reduceRight
D
Array.sort
17

Question 17

How do you supply a custom this context to forEach?

A
Pass the desired this as the second argument to forEach
B
Use bind on the callback
C
Use arrow functions automatically
D
It is impossible to change this
18

Question 18

What is the value of this inside an arrow function callback passed to forEach?

A
Arrow functions capture lexical this, ignoring the thisArg parameter
B
It equals the array being iterated
C
It always refers to window
D
It equals the callback itself
19

Question 19

What does the following code log?

javascript
const obj = {
  prefix: 'Item',
  log(value) {
    console.log(this.prefix, value)
  }
}
const values = [1, 2]
values.forEach(obj.log, obj)
A
Item 1 and Item 2
B
undefined 1 and undefined 2
C
Throws error due to this binding
D
Item Item
20

Question 20

Which pattern replicates the map behavior using forEach?

A
const result = []; array.forEach(value => result.push(transform(value)))
B
array.forEach(value => transform(value)) and discard result
C
array.forEach(transform, result)
D
array.map(transform).forEach
21

Question 21

Why might you choose a traditional for loop over forEach?

A
Normal for loops support break/continue and can be faster in performance-critical sections
B
for loops cannot mutate arrays
C
for loops cannot read indexes
D
for loops are deprecated
22

Question 22

What is a nested forEach commonly used for?

A
Iterating a parent array and then iterating an array property inside each element
B
Breaking out early
C
Sorting arrays
D
Memoizing functions
23

Question 23

What does this code output?

javascript
const matrix = [[1,2],[3,4]]
matrix.forEach(row => {
  row.forEach(value => console.log(value))
})
A
1 2 3 4 on separate lines
B
[[1,2],[3,4]]
C
Throws due to nested loops
D
Nothing, forEach returns undefined
24

Question 24

Which statement describes iterating objects inside arrays?

A
forEach can access each object and mutate its properties directly
B
Objects must be converted to arrays first
C
forEach clones objects before passing them
D
forEach cannot handle objects
25

Question 25

What is a common pattern for summing numbers using forEach?

A
let total = 0; array.forEach(value => total += value)
B
array.forEach(value => return value)
C
array.forEach(value => total = value)
D
array.forEach(() => total++)
26

Question 26

Which snippet logs indexes of truthy values?

javascript
const tags = ['ok', '', 'run']
tags.forEach((value, index) => {
  if (value) console.log(index)
})
A
Logs 0 and 2
B
Logs 0,1,2
C
Logs nothing
D
Throws error
27

Question 27

How do you handle asynchronous operations inside forEach?

A
forEach does not await promises; use for..of with await or Promise.all
B
forEach automatically awaits async callbacks
C
return inside forEach delays the loop
D
Use forEach with async/await inside
28

Question 28

What does console.table help with during forEach debugging?

A
It displays each object’s properties in a readable table, useful when iterating arrays of objects
B
It executes the loop faster
C
It stops the loop automatically
D
It returns a new array
29

Question 29

Which technique logs each element with its index?

javascript
items.forEach((value, index) => console.log(`${index}: ${value}`))
A
Displays "0: value0", "1: value1", etc.
B
Throws error because of template literals
C
Only logs indexes
D
Only logs values
30

Question 30

Which practice keeps forEach callbacks readable?

A
Extract the callback into a named function when it grows large
B
Nest callbacks indefinitely
C
Mutate global state directly
D
Avoid comments entirely
31

Question 31

How do you iterate an array of objects and collect a specific field?

javascript
const names = []
users.forEach(user => names.push(user.name))
A
names ends up with each user.name
B
names stays empty
C
forEach throws
D
It requires map instead
32

Question 32

What does this pattern accomplish?

javascript
const updates = []
todos.forEach(todo => {
  if (!todo.done) updates.push(todo.id)
})
A
Collects IDs of incomplete todos
B
Mutates todos to done
C
Throws due to if statement
D
Returns a new filtered array
33

Question 33

How do you emulate break inside forEach?

A
Use try/catch with throw, or switch to a for loop/some/every
B
Return false
C
Use continue
D
Use await
34

Question 34

What does Array.from + forEach enable when iterating over array-like objects?

A
Converts NodeLists/arguments into real arrays before forEach
B
Allows early exit
C
Sorts automatically
D
Clones objects deeply
35

Question 35

Which pattern compares forEach to a for loop?

javascript
array.forEach((value, index) => {
  // same as for (let i = 0; i < array.length; i++)
})
A
forEach handles iteration behind the scenes, similar to a for loop
B
forEach runs in parallel
C
forEach executes only once
D
forEach uses recursion internally
36

Question 36

How do you avoid callback hell when using nested forEach for complex transformations?

A
Refactor into separate functions or use map/reduce to express transformations
B
Keep nesting indefinitely
C
Use global variables everywhere
D
Use alert for debugging
37

Question 37

Which snippet demonstrates chaining the original array after forEach?

javascript
const arr = [1,2]
arr.forEach(v => console.log(v))
const doubled = arr.map(v => v * 2)
A
forEach logs values and arr remains usable for map afterward
B
forEach consumes arr
C
map cannot run after forEach
D
forEach returns the array to chain
38

Question 38

What is a best practice for handling errors inside forEach callbacks?

A
Wrap risky logic in try/catch if individual iterations can fail
B
Ignore errors entirely
C
Throw and crash the app
D
Only log errors
39

Question 39

Which approach helps keep forEach pure when necessary?

A
Avoid mutating external state inside the callback
B
Use global variables
C
Return new values
D
Throw errors on every iteration
40

Question 40

What is a recommended best practice for choosing iteration helpers?

A
Use forEach when side effects are intentional, and prefer map/filter/reduce when transforming data
B
Always use forEach for everything
C
Avoid all array helpers
D
Use forEach only in legacy code

QUIZZES IN JavaScript