LANGUAGE » JAVASCRIPT

Flow control

Condition

See if...else documentation.

js
if (10 < 20) {
  doSomething();
} else if (10 > 20) {
  doSomething();
} else {
  doSomething();
}

For loop

See for, for...in, for...of documentation.

js
for (let i = 0; i < 10; i++) {
    console.log(i);
}
// Loop dict
const mydict = { a: 1, b: 2, c: 3 };
for (const key in mydict) {
    console.log(`${key}: ${mydict[key]}`);
}
// Loop list
for (const element of [3, 4, 5]) {
  console.log(element);
}

While loop

See while documentation.

js
let price = 5;
while (price < 100) {
    if (price == 13) {
        price += 8;
        continue;  // Skip to next interation
    }
    if (price == 50) {
        break;  // Finish the loop now
    }
    console.log(`I may buy it for \$${price}`);
    price += 8;
}
console.log(`\$${price} is too expensive!`);

Do...while loop

See do...while documentation.

js
let price = 5;
do {
    console.log(`I may buy it for \$${price}`);
    price += 8;
} while (price < 100);
console.log(`\$${price} is too expensive!`);

Switch

See switch documentation.

js
switch (myExpr) {
  case '1':
    doSomething();
    break;
  case '2':
    doSomething();
    break;
  default:
    doSomething();
    break;
}

Try...catch finally

See throw, try...catch documentation.

js
try {
  throw new Error('Something is wrong!');
} catch (error) {
  console.log(error.message);
  if (error instanceof TypeError) {
    // Handle TypeError
  } else if (error instanceof RangeError) {
    // Handle RangeError
  } else if (error instanceof EvalError) {
    // Handle EvalError
  } else {
    throw error;
  }
} finally {
  // Will always run
}