Skip to content

Instantly share code, notes, and snippets.

@StevenLangbroek
Created October 11, 2018 16:51
Show Gist options
  • Save StevenLangbroek/c8c0abcacb9bce3b276e84f587d53674 to your computer and use it in GitHub Desktop.
Save StevenLangbroek/c8c0abcacb9bce3b276e84f587d53674 to your computer and use it in GitHub Desktop.
/**
* It's not hard to imagine JavaScript not having ternary statements,
* and having to implement these in userland. What would that look like?
* @param leftOrRight a maybe truthy value.
* @param left if truthy
* @param right otherwise
*/
function ternary(leftOrRight, left, right) {
if (leftOrRight) {
return left;
}
return right;
}
/**
* So:
* `prop.a || prop.b || prop.c ? 1 : 0`
* Becomes:
* `ternary(prop.a || prop.b || prop.c, 1, 0);`
* That whole first statement is a single value:
* `true`
* Because prop.a is falsy, but prop.b is truthy.
* Incidentally:
* You'd have to verify, but I think whether or
* not prop.c is even evaluated in this case is
* specified in the language.
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment