-2

I am a little confused about how truthy or falsy values are evaluated in angular. Apparently, when I do the following directly in angular... things work.

if (0) { console.log('not printed'); }
else { console.log('printed'); }

if (1) { console.log('printed'); }
else { console.log('not printed'); }

However, if I pass a value to my controller from the template... then things do NOT work. Why?

function (someValue) {

    if (someValue) { console.log('always printed  whether someValue is 0 or 1'); }
    else { console.log('not printed'); }

...
Grateful
  • 9,685
  • 10
  • 45
  • 77
  • 3
    Are you sure `someValue` is not the string `"0"`? – Marty Mar 27 '17 at 04:00
  • Yes, it is a string! Okay, I get. So this is not to do with angular after all.... regular javascript would also have had the same result? – Grateful Mar 27 '17 at 04:03
  • using `0` and `1` for true and false in JavaScript is very fault prone. – Claies Mar 27 '17 at 04:03
  • Yes, `!!"0"` in your console for example provides `true`. – Marty Mar 27 '17 at 04:04
  • Yes regular JS also would have the same effect. "0" is not the same as 0/null/undefined. – Aniket Inge Mar 27 '17 at 04:04
  • For the sake of trivia, PHP *does* treat `"0"` as a falsy value. – Marty Mar 27 '17 at 04:05
  • Use boolean type when possible to promote 'intentional' programming – Aniket Inge Mar 27 '17 at 04:05
  • So would the best way forward be to convert the string using `parseInt()` or would it be simply to do something like `if (someValue == false)`? – Grateful Mar 27 '17 at 04:11
  • the way forward depends on what is setting the value of `someValue`; seeing the binding to this property would help. – Claies Mar 27 '17 at 04:12
  • @Claies To answer you, someValue is either "0" or "1"... However, I would appreciate an elaboration on "depends on the what is setting" regardless. Thanks. – Grateful Mar 27 '17 at 04:16
  • is it being set by a text box? is it being set by a series of checkboxes? is it being set by a dropdown box? these each have a different way of handling true/false. – Claies Mar 27 '17 at 04:21

2 Answers2

0

When you compare, there is Loose equality and Strict equality in js. if the value is not a type of Boolean, it will be converted to either true or false

if (someValue === '0' || someValue === '1') { 
  console.log('always printed  whether someValue is 0 or 1'); 
}
else { 
  console.log('not printed'); 
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

Implicit data type conversion in JavaScript when comparing integer with string using ==

Community
  • 1
  • 1
Sabrina Luo
  • 3,810
  • 4
  • 16
  • 35
0

Generally I use a global function to deduce a truthy value.

function truthy(val){
  switch(typeof val){
    case "number":
      // since the val is already a number, only zero and NaN should be counted as falsy.
      return val !== 0 && !Number.isNaN(Number(val));
    case "string":
      // val is string, so need to check if its empty or "0". Otherwise it should be counted as truthy
      return val !== "" && val !== "0";
    case "boolean":
      // val is already boolean so only boolean check would suffice
      return val === true;
    case "symbol":
      //    symbol should be truthy
      return true;
    case "object":
      // object can be deceiving, so lets check for NULL and empty object

      if( val === null ){
        return false;
      }
      else{
        for( var i in val ){
          if( val.hasOwnProperty(i) ){
            return true;
          }
        }
      }

      return false;

    case "undefiend":
      return false;
    case "function":
      return true;
  }
}
function falsy(val){
  return !truthy(val);
}

console.log(truthy(1));     // should be true
console.log(truthy(0));     // should be false
console.log(truthy(NaN));   // should be false
console.log(truthy(""));    // should be false
console.log(truthy("0"));   // should be false
console.log(truthy("1"));   // should be true
console.log(truthy("meh")); // should be true
console.log(truthy({}));    // should be false
console.log(truthy(null));  // should be false
console.log(truthy(false)); // should be false

http://jsbin.com/wakiyej/edit?js,console

maksbd19
  • 3,785
  • 28
  • 37