Accessing deeper properties in objects

can someone tell me why in the mwe of the record collection task below, for the Basic JavaScript Curriculum from freeCodeCamp, the function updateRecords works with regard to the accessing of object and/or equivalently why the commented out lines do not work. To my newbie eye they appear equivalent

// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "Little Red Corvette" 
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    "5439": {
      "album": "ABBA Gold"
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
    var myid = collection[id];
    var myprop = myid[prop];

    if (myid.hasOwnProperty(prop) == false) {
    //if (collection[id].hasOwnProperty(prop) == false) {
    myid[prop] = value;
    //collection[id[prop]] = value;
    return collection;

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Not equivalent, look what’s inside the brackets in the two lines.

collection[id[prop]] <-- it is not this way that bracket notation works. Here it is reaching for the prop property inside the id object (which has a value of undefined), so the thing inside the brackets is evaluated and you have: collection[undefined], which is also undefined. value can’t be assigned to this.

Instead myid[prop] works. myid is collection[id] so total it is collection[id][prop]

Every time you have s doubt on what’s the value of something, console.log() it

Many thanks, that is most illustrative. To my mind then these brackets in the context of object properties behave more sequentially like the dot notation rather than say a functional notation. Coming from a maths/physics background this seems counter intuitive to me.

So let’s say we had an nested object Obj_0 that looked like (please excuse the pseudo JavaScript}

var Obj_0 =

then to access property prop_k with the bracket notation one would use the following syntax, correct?


That’s correct

It works in the same way as arrays
If you have a multidimensional array and you use arr[0][1][3] you are accessing the element at index 0 of the array, then the element at index 1 of that subarray, then the element at index 3 if that subarray