Skip to content

Instantly share code, notes, and snippets.

@DavidMellul
Created March 9, 2018 22:54
Show Gist options
  • Save DavidMellul/8f13bd6d1592d9a1f3806b804dc5b191 to your computer and use it in GitHub Desktop.
Save DavidMellul/8f13bd6d1592d9a1f3806b804dc5b191 to your computer and use it in GitHub Desktop.
// First way
let x = 3;
let sentence = (x > 1) ? 'X is above 1':'X is below 1';
console.log(sentence);
// => X is above 1
// Second shorter way, less-readable
console.log( (x > 1) ? 'X is above 1':'X is below 1' ); // Without a temporary variable
// The syntax is always the same, no matter the language
// (conditional statement) ? result-if-true : result-if-false
// You can even chain the ternary operators
console.log(
(x > 1) ?
(x > 2) ? 'X is above 2':'X is below 2'
:'X is below 1'
);
// => X is above 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment