If statement

Let year = prompt('In which year was ECMAScript-2015 specification published?', '');

*!*
if (year == 2015) alert( 'You are right!' );
*/!*

Gets a condition, evaluates it and, if the result is true, executes the code.

Else clause

Let year = prompt('In which year was ECMAScript-2015 specification published?', '');

if (year == 2015) {
  alert( 'You guessed it right!' );
} else {
  alert( 'How can you be so wrong?' ); // any value except 2015
}

The if statement may contain an optional "else" block. It executes when the condition is wrong.

Else if

Let year = prompt('In which year was ECMAScript-2015 specification published?', '');

if (year < 2015) {
  alert( 'Too early...' );
} else if (year > 2015) {
  alert( 'Too late' );
} else {
  alert( 'Exactly!' );
}

To test several variants of a condition.

If statement — Structure map

Clickable & Draggable!

If statement — Related pages: