while
vs do...while
do...while
is guaranteed to run the block once, since the condition is checked after execution of the block, whereas a while
statement will check the condition first meaning that the block may never be executed
for...in
vs for...of
for...in
will loop over the keys of an object, while for...of
will loop over the values
break
/ continue
It is possible to label code blocks like loops so that we can target them specifically when using break
or continue
statements. Here is an example taken from MDN
let i, j;
loop1:
for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1"
loop2:
for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2"
if (i === 1 && j === 1) {
continue loop1;
}
console.log(`i = ${i}, j = ${j}`);
}
}
// Logs:
// i = 0, j = 0
// i = 0, j = 1
// i = 0, j = 2
// i = 1, j = 0
// i = 2, j = 0
// i = 2, j = 1
// i = 2, j = 2