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
Post a Comment