Function that checks for truth value I was writing a function that takes an argument and returns a truth value.

My initial idea was just return !!value, but this will fail for '0', {} or []. I know that '0' should be true but in my application we are considering it false.

So, my function should return false for:


Falsey values: false, 0, undefined, null, ''
Blank values: {}, []
Special requirement: '0'


So considering it, I updated code to following to handle 



var isTrue = function(value) {
  switch (typeof(value)) {
    case "string":
      return !(value === '0' || value === 'false');
    case "number":
    case "boolean":
      return !!value;
    case "object":
      return Object.keys(value).length &gt; 0
  }
}

console.log(false,isTrue(false))
console.log(true,isTrue(true))
console.log(0,isTrue(0))
console.log(1,isTrue(1))
console.log('0',isTrue('0'))
console.log('1',isTrue('1'))
console.log('test',isTrue('test'))
console.log('Object',isTrue({}))
console.log('Array',isTrue([]))




Now this works fine. It does not include handling for functions but that is out of scope of current question. My question is, is there a better way to handle the same?
I thought it might be faster to:


first merely evaluate the value "standard" truth with !!value
then add conditions for your special requirements (including 'false', not cited in your question but present in your code)


This way, it also reduces the code, like this:



var isTrue = function(value) {
  return (!!value &amp;&amp; value !== '0' &amp;&amp; value !== 'false'
    &amp;&amp; !!(typeof value !== 'object' || Object.keys(value).length));
}

console.log(false,isTrue(false));
console.log("'false'",isTrue('false'));
console.log(true,isTrue(true));
console.log(0,isTrue(0));
console.log(1,isTrue(1));
console.log("'0'",isTrue('0'));
console.log("'1'",isTrue('1'));
console.log("'test'",isTrue('test'));
console.log('{}',isTrue({}));
console.log('[]',isTrue([]));
console.log('Object',isTrue({a: 1}));
console.log('Array',isTrue([1]));




Now we can observe that it works more than 2 times faster.
Here an execution of the same cases set as above, repeated 100 times:



var isTrue = function(value) {
  switch (typeof(value)) {
    case "string":
      return !(value === '0' || value === 'false');
    case "number":
    case "boolean":
      return !!value;
    case "object":
      return Object.keys(value).length &gt; 0
  }
}

var isTrue_2 = function(value) {
  return (!!value &amp;&amp; value !== '0' &amp;&amp; value !== 'false'
    &amp;&amp; !!(typeof value !== 'object' || Object.keys(value).length));
}

var values = [false, 'false', true, 0, 1, '0', '1', 'test', {}, [], {a: 1}, [1]],
    times = 100;

console.time('isTrue');
for (var i = 0; i &lt; times; i++) {
  for (var value of values) {
    isTrue(value);
  }
}
console.timeEnd('isTrue');

console.time('isTrue_2');
for (var i = 0; i &lt; times; i++) {
  for (var value of values) {
    isTrue_2(value);
  }
}
console.timeEnd('isTrue_2');




Last point of interest: if you can have some statistics about how much the special requirements appear, you may change the order of the conditions to put the most frequent cases first, so yet improving performance.