Full-stack Web Technologies

CHAPTER 8
If

If

Evaluates the condition, and if the resulting value is true, executes the code inside the braces.

if (password === '123456') {
  console.log('You are now logged in.');
}

If-else

if (password === '123456') {
  console.log('You are now logged in.');
} else {
  console.log('Wrong user/password combination.');
}

If-else-if

if (password === '') {
  console.log('The password is empty.');
} else if (password === '123456') {
  console.log('You are now logged in.');
} else {
  console.log('Wrong user/password combination.');
}

Boolean Conversion

When the if condition is not a boolean, the value is auto-converted (once again), but this is not so bad:

if (a)numbera != 0if a is not zero
if (s)strings.length > 0 or s !== ""if s is not empty
if (x)objects !== null or s !== undefinedif x is not null or undefined

Switch

switch (value) {
  case v1: 
    // ...
    break;
  case v2:
    // ...
    break;

  // ... more cases ...

  default:
    // ...
}