IdentifierAsStringInBracketNotation
DRAFT

Misconception:

When accessing a property with the bracket notation as in obj[foo], foo is treated as the string "foo".

Incorrect

An identifier used to access a property with the bracket notation is treated as a string

Correct

An identifier used to access a property with the bracket notation is firstly evaluated

Correction
Here is what's right.

If one uses an identifier, such as a variable name, inside the bracket notation, that identifier is firstly evaluated and the resulting value is used to access the property.

Consider the following code:

Here domain is an identifier which is resolved before attempting to access a property of obj and, since there is no definition for domain, we get a ReferenceError.

What one probably wants is to use a string inside the brackets, which can be either a string literal or a variable that contains a string, as demostrated in the next example.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.