In the record collection hint where the solution is presented, it would be more clear if the solution also mention the use of dot notation to be only used for static keys and bracket notion for dynamic keys.
Can you link the challenge so we fully know what this is in reference to?
yes but no where does it mention I shouldn’t be using dot notation. I have been using dot natation for most of my practice.
To access the value of a key in this object, you will use bracket notation:
records[id][prop]
.
Hi @isuravya ,
It is important to note that obj.key
is not the same as obj[key]
.
let user = {
fname: "Chavo"
}
Try it in your browser’s console after pasting the object above inside:
user.fname
user[fname]
The first dot notation method assumes that you know the name of the key beforehand. The second method, though, looks for a variable called fname
:
let fname = “fname”
If the variable fname
does not exist, then user[fname]
will return undefined
. To access the key "fname"
using bracket notations, you would say user ["fname"]
(remember that object keys are inherently strings. It is just that we are allowed to omit the string when using the dot notation, or while crating the object {key: "value"}
.
Now here is a question for you. What are the results of these assignments?
user.age = 20
user[gender] = "male" // Assume that gender is not declared.
For more information, take a look at the article below: