if Keyword

Examples

Using if...else

if (cipherChar === fromChar) {
  result += toChar;
  x++;
} else {
  result += clearChar;
}


Using else if

Note that there is no elseif syntax in JavaScript. However, you can write it with a space between else and if:

if (x > 50) {
  /* do something */
} else if (x > 5) {
  /* do something */
} else {
  /* do something */
}


Using an assignment as a condition

You should almost never have an if...else with an assignment like x = y as a condition:

if (x = y) {
  /* do something */
}

However, in the rare case you find yourself wanting to do something like that, the while documentation has a Using an assignment as a condition section with an example showing a general best-practice syntax you should know about and follow.