Confusion about square bracket

why dot notation is not working here, when i try person.x inside for loop?


<script>
var x, txt = "";
var person = {name:"John", age:50, city:"New York"};

for (x in person) {
  txt += person[x] + " ";
};

document.getElementById("demo").innerHTML = txt;
</script>

You cannot use dot notation with a variable. Dot notation looks for the property x instead of the property that matches the contents of x.

1 Like

For quick reference, here are the challenges where bracket notation, dot notation, and using variables in object lookups are introduced:

2 Likes