Thursday 20 April 2017

How to always return truthy using the least characters on an expression?

Given:

(1);
(0);
("a string");
("");
(undefined);

What can be put in front of the parenthesis to always return a truthy value.

A function defined as

var a = () => true; 

will work. So placing the letter a in front of each item in the list will have each one always return true since this creates a function call to "a". However this requires calling a function every time.

a(1);
a(0);
a("a string");
a("");
a(undefined);

Is there a more efficient way to do this. I tried !! which won't work on falsey values. Also tried ~ tilde but that won't work on (-1) resulting in 0. Any other symbols that could be used to always return a truthy value or is a function the only way to do this?

Looking for an answer that uses 1 or 2 chars, but doesn't involve calling a function. Using node.js so ES6 answers would work if there is one.



via esnm

No comments:

Post a Comment