My first question on here and need help understanding the for in loop in JavaScript.
When I run the following code I get "undefined" from the alert function:
var o = { a: "property 1", b: "property 2", c: "property 3", d: "property 4" } for (p in o) { alert(o.p); } but if I were to change . to [ ] (ie. alert(o[p])) the alert would return the property value as expected.
Why can't I use . to access the object property?
pinstead of a property namedp. Therfore the.operator is specifically for names of property identifiers (whether or not they exist), whereas[]is for using the result of any expression as the property name.