Today I tried shortening an if statement like this:
if (fruit == "apple" || type == "pear" || type == "orange"){
to this:
if (fruit in ["apple", "pear", "orange"]){
It doesn't work. Jonathan Snook has a different solution here where he essentially uses a map like this, which does work:
if(fruit in {"apple":"", "pear":"", "orange":""}){
Why does that work and my simple array doesn't, when usually in Javascript an object's presence makes that object return true? Is a string a different type of object than a key? I thought the string's presence in my array would return true as well.
y = "Stackoverflow";
y && console.log('y is true') // y returns true, so console logs the message
I interpreted my original solution as "if the value of variable fruit is in this array." That's clearly not the case. Does it instead say "if the variable fruit itself is in this array?" No, because this doesn't work either:
if (fruit in [fruit, "apple", "pear", "orange"]){
So what is Snook's version with the key=>value map asking that's correct? My best guess is, "if a key under the name of the value of variable fruit in this map returns true?"