
Bite-size coding challenge: implement if/else in JavaScript without `if` or `?:` - markchristian
Here&#x27;s a bit of Friday fun for you: your mission, should you choose to accept it, is to implement if&#x2F;then logic in JavaScript without using `if` or the ternary operator `?:`.<p>Implement a function with the following signature:<p>````
function ifThenElse(cond, trueFn, falseFn) { }
````<p>If `cond` is truthy, execute `trueFn`, otherwise execute `falseFn`. `ifThenElse` should not have a return value.<p>Here&#x27;s a gist that contains a very tiny test harness:
https:&#x2F;&#x2F;gist.github.com&#x2F;shinypb&#x2F;befb5363395c754758b157b850cf747a<p>Put your implementations into your own gist and then reply to this thread.<p>I&#x27;m curious to see what sort of horrible atrocities you can all come up with. :) Happy hacking!<p>PS: Here are my two ideas:<p>https:&#x2F;&#x2F;gist.github.com&#x2F;shinypb&#x2F;85d6dd19e712f4d7962bd4c0cb30dfec<p>https:&#x2F;&#x2F;gist.github.com&#x2F;shinypb&#x2F;78dd126d946bec6cc77245d40d0748cb
======
elliotbnvl

      function ifThenElse(cond, trueFn, falseFn) {
          ({ true: () => trueFn(), false: () => falseFn() })[cond]();
      }

------
Someone

      function ifThenElse(cond, trueFn, falseFn) {
        cond && trueFn();
        cond || falseFn();
      }

