If you take a closer look at this for in loop:
var obj = {
a: "property 1",
b: "property 2",
c: "property 3",
d: "property 4"
}
for (var prop in obj) {
if (typeof(prop) == 'string') {
console.log('This is a string!');
}
}
You will notice that the what the for in loop is giving you is the name of the property.
That's why it would be ok to use obj[prop]
because the property would be a string and that is a legal way to access a property of an object.
When you try and execute obj.prop
it gives you undefined because property is a string that represents the name of a property, it's not the actual property.