I think it will be most simple to replace the variables with constants to see what is actually going on in each loop:
The first loop:
for (prop in obj){
console.log(prop + " scored " + obj[prop][0] + " out of " + obj[prop][1] + " shots.");
}
Iteration 1:
console.log("Laser" + " scored " + obj["Laser"][0] + " out of " + obj["Laser"][1] + " shots.");
Iteration 2:
console.log("Missile" + " scored " + obj["Missile"][0] + " out of " + obj["Missile"][1] + " shots.");
The second loop:
for (prop in obj){
console.log(prop + " scored " + obj.prop[0] + " out of " + obj.prop[1] + " shots.");
-> error
Iteration 1:
console.log("Laser" + " scored " + obj["prop"][0] + " out of " + obj["prop"][1] + " shots.");
Iteration 2:
console.log("Missile" + " scored " + obj["prop"][0] + " out of " + obj["prop"][1] + " shots.");
By using dot notation you are asking to look for a property named prop
on the obj
. This is obviously not what you are after.
So these are equivalent:
obj.prop
obj["prop"]