Are quotes always needed for bracket notation?

While doing the "Updating Object Properties " challenge, I notice that it the code does not pass with the bracket notation if I do not use quotes. However, the lessons only say the quotations are needed if there is more than one word in the string.

So, just to clarify, are quotations always needed when I want to access an object’s property via bracket notation when the property is a string?

Challenge: Updating Object Properties

Link to the challenge:

If there are no spaces and you are calling the attribute by its explicit name, like ourDog.name then that works just fine. However, if ourDog had a property named something like “favorite things”, then the syntax ourDog.favorite things would fail, because “things” would be seen as a completely separate token. In this case, you may use bracket notation, but whether there are spaces or not you always use quotations with string literals in bracket notation, because otherwise they will be interpreted as variables.

For instance, you could do something like this:

let propName = "favorite things";
console.log(ourDog[propName]);

and that would accomplish the same thing as:
console.log(ourDog["favorite things"]);

A different example, the following is also valid code:

let keys = Object.keys(myDog);
for (let i=0; i < keys.length; i++)
  console.log(myDog[keys[i]]);

Not that that is something you would want to do, just an example; in brackets it is assumed to be a variable, so you have to put quotations around it to make it a string literal.

here is an object:

obj {
  "prop name": "prop value one",
  propName: "prop value two",
  key: "key value"
}

let propName="key"
console.log(obj.propName)  //"prop value two"
console.log(obj.["propName"])  //"prop value two"
console.log(obj.[propName])  //"key value"
console.log(obj.["key"]) //"key value"
console.log(obj.[key]) // undefined(variable key is not defined)
console.log(obj.["prop name"])  //"prop value one"
console.log(obj."prop name") //invalid syntax

You are confusing the explanation about rules for property names and rules for referencing them. When you declare a property inside an object, which is more than a word, you need to represent its value as a string, using quotes. For exampe the property “prop name” need to be in quotes. propName however dont have to be, because its one word. So i declared propName and key properties without using quotes, but for prop name i dont have this option.
Regarding reference, to access propName value, we can say obj.propName, or obj["propName"]. We can use dot notation, when we know beforehand the name of the property, however, when the property name is more than one word, we must use bracket notation: obj["prop name"]. Bracket notation evaluates the expression inside the brackets and then uses the value as a reference for the property name. If i say obj["propName"], the value inside the brackets is the string “propName” and the reference leads to that property in the object, but if i write obj[propName], it will look for the value inside the propName variable, which in the end would be equal to obj["key"], as i declared the propName variable value equal “key”, thus the reference will lead to that object property. Like i said, brackets evaluates whats written within, unlike dot notation, which leads to the explicit value. You could even write obj["prop"+" "+"name"] to access the "prop name" property, or use other variables/expression to produce that value.

1 Like

Okay. I get it now. Thanks

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.