What is special about let i in for (let i = 0; i < 3; i++)?
The engine copies the value into a fresh binding for every iteration. That is why setTimeout(() => console.log(i)) prints 0, 1, 2 with let and 3, 3, 3 with var.
Loops · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript loops: for, while and do…while, for…of vs for…in, iterables, break, continue and labels, forEach, sparse arrays and key order. Pick an option to check your answer instantly.
What is special about let i in for (let i = 0; i < 3; i++)?
The engine copies the value into a fresh binding for every iteration. That is why setTimeout(() => console.log(i)) prints 0, 1, 2 with let and 3, 3, 3 with var.
What does return do inside a forEach callback?
It acts like continue. forEach always visits every element and itself returns undefined.
How many times does [1, , 3].forEach(fn) call fn?
forEach, map, filter, reduce and for…in skip holes. A classic for loop and for…of visit index 1 and read undefined.
You need to know whether any order is above ₹10,000 and want to stop checking as soon as one is found. Which method fits?
some returns true at the first element that passes and stops. forEach, map and filter always visit every element.
"hi😀".length is 4. How many times does for (const ch of "hi😀") run?
The emoji uses two UTF-16 code units (so length counts 2), but the string iterator yields whole code points. A classic for loop with str[i] would split it in two.
Timed online tests with instant scores and explanations.