logical operators - Not expected behavior while setting a string with short-circuit evaluation in Javascript -


i want use short-circuit evaluation report nice status of multiple items in 1 liner. result not expected shown below:

var items = [{      "id": 1,      "available": true  }, {      "id": 2,      "available": false  }, {      "id": 3,      "error": "server not found tld"  }];    items.foreach(function(item) {  	console.log(item.id, item.error || item.available ? "available" : "not available");  });

this produced following log:

1 "available" 2 "not available" 3 "available" 

at 3 expected show error because item.error string , should evaluate `true, why skip item.available?

item.error || item.available truthy.

you need parentheses:

item.error || (item.available ? "available" : "not available") 

Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -