Statements

JavaScript statements.

Declarations

let
Block-scoped variable
{
  let someVar;
  let someOtherVar = 10;

  {
    someVar = 20; // sub-scopes have access to variables from parent scope
    let subScopedVar = 40;
  }

  // subScopedVar => undefined, since it was defined in a child scope
}
const
Block-scoped constant
{
  const someVar; // NOT VALID, const variables must be assigned at declaration
  const someOtherVar = 10;

  {
    someOtherVar = 20; // NOT VALID, can't assign a value to a const variable
    const subScopedVar = 40;
  }

  // subScopedVar => undefined, since it was defined in a child scope
}

Flow control

try...catch
Handle errors
try {
  // do something 'risky'
} catch (error) {
  // gracefully handle error, return some default
}
if...else
Conditional
if (someCondition) {
  // do something
} else if (anotherCondition) {
  // do something else
} else {
  // just do this
}
switch
Conditional
switch (someInput) {
  case firstValue:
    // do something when `someInput === firstValue`
    break; // exits this switch-case and continue execution after the switch
  case secondValue:
  // do something when `someInput === secondValue`

  // without a `break` execution will "fall-through" to the next block even if
  // `someInput !== thirdValue`
  case thirdValue:
    // do something when `someInput === thirdValue` (or since there's no `break`
    // above, also `someInput === secondValue`)
    break;
  case fourthValue:
  case fifthValue:
    // do something when `someInput === fourthValue || someInput === fifthValue`
    return anotherValue; // can be used in place of break, this will return
  // `anotherValue` to the caller
  default:
    // do something if `someInput` hasn't matched any of the other cases
    break;
}

Iteration

for
standard for-loop
for (let i = 0; i < 9; i++) {
  // do something while `i` iterates through [0, 1, 2, 3, 4, 5, 6, 7, 8]

  if (someCondition) {
    break; // exit early if some `someCondition` happens
  }

  if (someOtherCondition) {
    continue; // stop executing this loop and start next iteration (don't execute MORE below)
  }

  /* MORE */
}
for...in
object property for-loop
const someObject = { first: 1, second: 2, third: "three" };
for (const key in someObject) {
  // do something while `key` iterates through ['first', 'second', 'third'] (order not guaranteed)
  const currentValue = someObject[key];

  if (someCondition) {
    break; // exit early if some `someCondition` happens
  }

  if (someOtherCondition) {
    continue; // stop executing this loop and start next iteration (don't execute MORE below)
  }

  /* MORE */
}
for...of
iterable for-loop
const someArray = ["a", "b", "c"];
for (const item of someArray) {
  // do something while `item` iterates through ['a', 'b', 'c']

  if (someCondition) {
    break; // exit early if some `someCondition` happens
  }

  if (someOtherCondition) {
    continue; // stop executing this loop and start next iteration (don't execute MORE below)
  }

  /* MORE */
}
while
while-loop
let done = false;
while (!done) {
  // do something

  if (someCondition) {
    break; // exit early if some `someCondition` happens
  }

  if (someOtherCondition) {
    continue; // stop executing this loop and start next iteration (don't execute MORE below)
  }

  /* MORE */

  if (finallyCondition) {
    done = true;
  }
}
do...while
do-while-loop (like a while-loop, except the body executes at least once)
let done = true;
do {
  // do something
  done = false;

  if (someCondition) {
    break; // exit early if some `someCondition` happens
  }

  if (someOtherCondition) {
    continue; // stop executing this loop and start next iteration (don't execute MORE below)
  }

  /* MORE */

  if (finallyCondition) {
    done = true;
  }
} while (!done);