Created
March 9, 2018 22:54
-
-
Save DavidMellul/8f13bd6d1592d9a1f3806b804dc5b191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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