JS else if Statement Help

Hello, I would simply like to know if these two statements would do the same thing.

The differences are that the first has an if, if else, and an else statement while the second has an if, two if else statements, and no else statement.

FIRST

if (time < 10) {
greeting = “Good morning”;
} else if (time < 20) {
greeting = “Good day”;
} else {
greeting = “Good evening”;
}

SECOND

if (time < 10) {
greeting = “Good morning”;
} else if (time < 20) {
greeting = “Good day”;
} else if {
greeting = “Good evening”;
}

Hi @Nathaniel_Rod.

The two code blocks are basically equivalent, except that the second one won’t work — else if has to have a condition after it in parens, otherwise you get a syntax error.

This would work:

if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else if(time >= 20) {
  greeting = "Good evening";
}

The semantics of else and else ifdiffer as follows:

  • else is basically saying “if none of the other conditions above are true, then execute this bit of code”. It is basically a default option to execute if all the others fail.
  • else if is basically saying “if this specific condition is true, then execute this bit of code”. It is provided as a specific alternative to the original if() { } option.

[Bug 600692 - Implement Content Security Policy (CSP) for Bugzilla

Bug 600692 - Implement Content Security Policy (CSP) for Bugzilla