Challenge 1 +100 XP
Write a `for...of` loop that prints each name in the `guests` array.
- Test 1 — expects "Alice\nBob\nCharlie"
Show solution (0 XP)
const guests = ['Alice', 'Bob', 'Charlie'];
for (const guest of guests) {
console.log(guest);
} JavaScript provides several ways to loop through the elements of an array. The two most common and modern approaches are the array method forEach() and the language statement for...of. While they often achieve the same result, they work differently under the hood. Understanding these differences is crucial for writing clean, bug-free code, especially when you need to stop a loop early or perform asynchronous operations.
The forEach() method of Array instances executes a provided function once for each array element.
The forEach() method is a higher-order function, meaning it takes another function (a callback) as an argument. It calls this provided function once for every item in the array. This approach is highly declarative—you tell JavaScript what to do for each item, rather than how to loop through them step-by-step.
const items = ['a', 'b'];
items.forEach(item => {
console.log(item);
});a b
While forEach is an array method, for...of is a standard looping construct built into the language itself. It works on any iterable object, not just arrays. This imperative style gives you more control over the loop’s execution flow, making it easier to read for complex logic and allowing you to pause or stop the loop at any time.
The for…of statement executes a loop that operates on a sequence of values sourced from an iterable object.
const items = ['x', 'y'];
for (const item of items) {
console.log(item);
}x y
You can use break, continue, and return statements in a for…of loop.
const items = [1, 2, 3, 4];
for (const item of items) {
if (item === 3) break;
console.log(item);
}1 2
There is no way to stop or break a forEach() loop other than by throwing an exception.
const items = [1, 2, 3];
try {
items.forEach(item => {
if (item === 2) break;
console.log(item);
});
} catch (e) {
console.log(e.name);
}SyntaxError
SyntaxError
currentValue: The current element being processed in the array. index: The index of the current element.
Read the code. What exactly will it print? Commit to an answer before you look.
const letters = ['a', 'b'];
letters.forEach((letter, index) => {
console.log(index + ': ' + letter);
});0: a 1: b
const arr = [];
arr[2] = 'c'; // sparse array
arr.forEach(x => console.log('forEach seen: ' + x));
for (const x of arr) console.log('for...of seen: ' + x);Can you use a 'break' statement inside a forEach loop?
No, forEach does not support break — The 'break' statement is not valid inside a forEach callback function. If you need to stop early, use a for...of loop instead.
What does the forEach method return?
undefined — forEach always returns undefined. It is used solely for executing side effects, not for transforming arrays.
How do you access the current index inside a forEach loop?
It is the second argument to the callback — The forEach callback receives the current value as the first argument, and the current index as the second argument.
Write a `for...of` loop that prints each name in the `guests` array.
const guests = ['Alice', 'Bob', 'Charlie'];
for (const guest of guests) {
console.log(guest);
} This code is supposed to log only the even numbers using `forEach`, but it throws a SyntaxError because it tries to use `continue`. Fix the code.
This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.
const numbers = [1, 2, 3, 4];
numbers.forEach(num => {
if (num % 2 !== 0) {
return;
}
console.log(num);
});