2

Why my "C" condition go to 'else' statement?, they separated are going to 'if' statement but together are not working.

var objTest = {
    ID : "10"
};

//A: First Condition: Exist value in property ID
console.log((objTest.ID ? 'if' : 'else'));                      // output => "if"

//B: Second Condition: Value different from "0"
console.log((objTest.ID != "0" ? 'if' : 'else'));               // output => "if"

//C: First and Second Condition together must be "if" 
console.log((objTest.ID & objTest.ID != "0" ? 'if' : 'else'));  // output => "else"
cнŝdk
  • 31,391
  • 7
  • 56
  • 78

1 Answers1

2

Your problem is that you are using the wrong ANDopernator, you need to use && instead of &:

console.log((objTest.ID && objTest.ID != "0" ? 'if' : 'else'));

The first &is a bitwise operator and &&is the logical operator.

Please take a look at What's the difference between & and && in JavaScript? for further details.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78