0

Given:

(1);
(0);
(-1);
("a string");
("");
(null);
(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 (a thruthy value) since this creates a function call to "a". However this requires calling a function every time.

a(1);
a(0);
a(-1);
a("a string");
a("");
a(null);
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 and two tildes ~~ but that won't work on (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.

ciso
  • 2,887
  • 6
  • 33
  • 58

2 Answers2

3

As long as the other side of the expression gets converted to a number (which I think it always does), 1 | x should never be able to return 0. So you can use that :

console.log(1|(1),
1|(0),
1|(-1),
1|("a string"),
1|(""),
1|(null),
1|(undefined),
1|(console.log(3)),
1|(Infinity))

Got the idea from : Bitwise operations on non numbers

Community
  • 1
  • 1
juvian
  • 15,875
  • 2
  • 37
  • 38
  • `true` is not logged for the expressions? – guest271314 Apr 20 '17 at 17:55
  • @guest271314 he wanted a truthy value, not true – juvian Apr 20 '17 at 17:55
  • Ah, did not notice that part of requirement. – guest271314 Apr 20 '17 at 17:56
  • 1
    I knew this... :( but i thought you need "true" – Rohith K P Apr 20 '17 at 18:00
  • @RohithKP I thought that as well but asked in guest271314 answer comments to confirm :) – juvian Apr 20 '17 at 18:02
  • After running some tests, this doesn't look any more efficient than the function call I described in the original post. I guess the bitwise operator results in a function call to perform the bit manipulation. – ciso Apr 20 '17 at 19:35
  • @esnm that's interesting. Bit operations should be pretty fast, but maybe the casting to integer is what takes long. Still, how did you measure? It's hard to compare because you don't have to consider the time to run right side expressions – juvian Apr 21 '17 at 04:06
  • @juvian Ran the same right side expression a billion times using a function call and the bitwise operator. Each took 2.0 seconds to complete. Pgm: "var a = () => true, i; for (i = 0; i < 1000000000; i +=1) {1|("a string");} i;" // returns 1,000,000,000. Then changed the 1|() to a(). Same execution time. Used Google Chrome console to run the test. – ciso Apr 22 '17 at 00:02
  • @esnm yeah seems to be the same function cost – juvian Apr 22 '17 at 17:40
1

You can use comma operator, include true following first portion of expression

console.log((1, true),
(0, true),
("a string", true),
("", true),
(undefined, true));

You can alternatively use Logical NOT !, Sign-propagating right shift >>, and 1

console.log(!((1) >> 1),
!((0) >> 1),
!(("a string") >> 1),
!(("") >> 1),
!((undefined) >> 1));

which can also be composed to meet requirement of preceding operator or expression by using !(0 >> (<expression>))

console.log(!(0 >> (1)),
!(0 >> (0)),
!(0 >> ("a string")),
!(0 >> ("")),
!(0 >> (undefined)));
guest271314
  • 1
  • 15
  • 104
  • 177