Record Collection question...,

Hi there, I am currently doing Record collection in Basic Javascript and have a question.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection[id][prop]) {
    collection[id][prop].push(value);
   }
   else {
    collection[id][prop]=[value];
   }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

  return collection;
}

In this code, what is different between
collection[id][prop].push(value); and collection[id][prop] = value; ?

Hi,

collection[id][prop].push(value); - this will add the value to the end of the array.

collection[id][prop] = value; - will replace the existing value of collection[id][prop] if it exists or will create it if it not exists yet

Additionally, I would like to add that the push() is an array method. So it can be applied to only arrays.
Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

And here is a small example to better understand the difference:

let collection = {1: "a", 
                  2: {1: [1, 2, 3]}, 
                  3: {1: "d"}
                 };

console.log(collection); 

collection[2][1].push(4,5);

collection[3][2] = "John";

console.log(collection);

collection[3][1] = "Alex";

console.log(collection);

The output of the first console.log() will be:

[object Object] {
  1: "a",
  2: [object Object] {
    1: [1, 2, 3]
  },
  3: [object Object] {
    1: "d"
  }
}

the second output will be:

[object Object] {
  1: "a",
  2: [object Object] {
    1: [1, 2, 3, 4, 5]
  },
  3: [object Object] {
    1: "d",
    2: "John"
  }
}

the third output will be:

[object Object] {
  1: "a",
  2: [object Object] {
    1: [1, 2, 3, 4, 5]
  },
  3: [object Object] {
    1: "d",
    2: "John"
  }
}

Thanks a lot!! now I understand clearly !!