I'm coming to javascript from a python background and wanted to ask about how to interpret some code i see.
In Python, I can get the following code/output:
Python Code:
myarray = ["a","b", "c"]
for item in myarray:
print (item)
Python Output
a
b
c
In Javascript, this gives me something different:
Javascript Code:
var myarray = ["a","b","c"]
for(var item in myarray){
console.log(item)
}
Javascript Output:
"0"
"1"
"2"
It's this interpretation that's confusing me. In python, the for loop naturally reads "for every item in myarray, print item". However in the Javascript version, it prints out "0", "1", "2". To get it correct, I would need to change the code to be:
var myarray = ["a","b","c"]
for(var item in myarray){
console.log(myarray[item])
}
I wanted to ask what is the logic behind this as (at least to me, it doesn't seem as clear)? Also, why would my first way print the items out as strings?
Thank you for your help!