Skip to content

Instantly share code, notes, and snippets.

@jonaskahn
Last active March 6, 2023 11:16
Show Gist options
  • Save jonaskahn/02b45a0ddbd8de543de0ad0e6c79129a to your computer and use it in GitHub Desktop.
Save jonaskahn/02b45a0ddbd8de543de0ad0e6c79129a to your computer and use it in GitHub Desktop.

The “if” statement

  • Ex 1:
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');

if (year == 2015) {
  alert( 'You guessed it right!' );
} else {
  alert( 'How can you be so wrong?' ); // any value except 2015
}
  • Ex2:
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');

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

Conditional operator ‘?’

  • Ex3 :
let accessAllowed;
let age = prompt('How old are you?', '');

if (age > 18) {
 accessAllowed = true;
} else {
 accessAllowed = false;
}

alert(accessAllowed);
let accessAllowed = (age > 18) ? true : false;

Questions

if (1 || 0) {
  alert('I'm right')
}


if (0 || '0') {
  alert('I'm right')
}


if (0 || ' ') {
  alert('I'm right')
}


if (0 || '') {
  alert('I'm right')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment