loops - Weird if-else behaviour Javascript -


this question has answer here:

the else-statement below never executes if if-statement false. think i've made basic mistake can't figure out what.

var = ["king","queen","100"];     (var i=0; i<a.length; i++) {         if (a[i] === "king" || "queen"){             console.log("monarch");         }         else {             console.log("the number is: "+ parseint(a[i]));         }     } // prints out "monarch" 3 times 

should be:

var = ["king","queen","100"]; (var i=0; i<a.length; i++) {     if (a[i] === "king" || a[i] ===  "queen"){         console.log("monarch");     }     else {         console.log("the number is: "+ parseint(a[i]));     } } 

you wrote boolean expression way speak spoken language, "either 1 or 2". that's not way or interpreted.

either left side of or true: a[i] === "king" true; or right side of or true: "queen". it's evaluating string itself, , string "queen" not null, evaluates true.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -