Thursday, January 19, 2017

JavaScript: Ternary Conditional

Some simple conditionals take a lot of code to choose on assignment.
let isAuthor = false;
let weapon;

if (isAuthor) {
  weapon = "Exalibur";
} else {
  weapon = "Longsword";
};
Let's use a syntax trick to make this more concise!

The ternary conditional provides a shortcut over lengthier conditional blocks. Ternary conditional syntax:
someCondition ? pickThisIfTrue : pickThisIfFalse ;
Let's turn our lengthy conditional example to ternary conditional.
let isAuthur = false;
let weapon = isAuthor ? "Exalibur" : "Longsword";

No comments:

Post a Comment