(Ad, please don’t block.)

Quizzes » Control flow statements

1. Syntax

Which of these are legal?

// 1
if (x === 0) {
  foo(x);
}

// 2
if (x === 0) {
  foo(x);
} else {
  bar(x);
}

// 3
if (x === 0) {
  foo(x);
} else if (x < 0) {
  bar(x);
}

// 4
if (x === 0) foo(x);

// 5
if (x === 0) foo(x);
else bar(x);

// 6
if (x === 0) foo(x);
else if (x < 0) bar(x);

2. break

Which of the following statements are legal?

{
  break; // 1
}

label1: {
  break label1; // 2
}

while (true) {
  break; // 3
}

label2: while (true) {
  break label2; // 4
}

3. switch

const x = 0;
let result = '';

switch (x) {
  case 0:
    result += 'zero';
  case 1:
    result += 'one';
  default:
    result += 'multiple';
}

What happens?

4. Infinite loops

Which of the following loops are infinite?

// 1
while () {}
// 2
while (true) {}

// 3
do {} while ();
// 4
do {} while (true);

// 5
for () {}
// 6
for (;;) {}

5. Loops that never execute their bodies

Which of the following loops never execute their bodies?

// 1
while (false) {
  console.log('while');
}

// 2
do {
  console.log('do-while');
} while (false);

// 3
for (;false;) {
  console.log('for');
}

6. for-of

const result = [];

const arr = ['a', 'b', 'c'];
for (const [index, elem] of arr.entries()) {
  if (index > 1) break;
  result.push(elem);
}

What happens?


Correct answers: 0 out of 0