Dot notation for accessing property de

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Setup
var recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
console.log(records[id])
return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Record Collection

Link to the challenge:

Hey,
so I was trying to console.log(records.id) and it shows undefined. Whereas when I tried the earlier exercises on nested objects I used the same notation and had no problem.
But surprisingly enough, console.log(records[id]) works.
Could someone point me towards what I am missing here?
Thanks in advance.

Hey There @bhishmadas9
If you try to use dot notation with the variable id, the browser will search for a property in the object called "id"

In bracket notation, you must use “” when mentioning a object property name.
Bracket notation also allows you to use variable like id in this case
To use a variable to access an object property, you must not use “”
object[id] //This searches for the value of variable id

object.id // This searches for the property "id" in the object

Hope this helped
Happy Coding

1 Like

hey,
Thanks for the quick reply. Really appreciate it.
But could you please tell me why console.log(records.id) doesn’t work?
Sorry, I am a bit confused here. Would be grateful if someone clarified.
Thanks.

Check the first line of my reply
The browser will try to find property id in the object
Which will return undefined

If you try this:

records.id

… it will look if the records object has a property that is literally called id. Like this:

const records = {
    id: 1234
}

But the id in this challenge is a variable. It can have many different values. So you can’t access the property with dot notation, you need bracket notation:

records[id]
2 Likes

Thanks y’all and happy hacking :grinning:

Hey y’all,

Is it correct to understand that the dot notation can’t deal with variables and the bracket notation should be used for that instead?

Yes exactly. You use dot notation when you know the exact literal name of the property you’re trying to access (you can also use bracket notation with quotes, see second example below, and you must use it when the property begins with a number or contains white space).
If you have the property name stored in variable, you have to use bracket notation without quotes.

const obj = {
  prop: 'some property'
}

const propName = 'prop';

obj.prop      // 'some property'
obj['prop']   // 'some property'
obj[prop]     // Uncaught ReferenceError: prop is not defined
obj[propName] // 'some property'

Better Explanation than mine, I guess @jsdisco:laughing:

It’s thanks to you both that I solved the problem…Thanks :grinning_face_with_smiling_eyes:

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