You address arrays by using brackets, "", as shown in the following example. The brackets enclose either a numeric value, or an expression that evaluates to a whole number.
Normally, you use the dot operator "." to access an object's properties. For example,
Here, the property name is an identifier. You can also access an object's properties using the index operator "". Here, you are treating the object as an associative array. An associative array is a data structure that allows you to dynamically associate arbitrary data values with arbitrary strings. For example,
Although the use of the index operator is more commonly associated with accessing array elements, when used with objects, the index is always the property name expressed as a string literal.
Notice the important difference between the two ways of accessing object properties.
The property name is treated as
Meaning the property name
cannot be manipulated as data
a string literal
can be manipulated as data
This difference becomes useful when you do not know what the property names will be until runtime (for example, when you are constructing objects based on user input). To extract all the properties from an associative array, you must use the for in loop.