Record Collection || question

Tell us what’s happening:
I looked up the hints for this exercise and trying to understand the meaning / what the following line does

collection[id][prop] = collection[id][prop] || ;

the code won’t work without it but not sure why it’s needed.

Your code so far

// 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"

// Only change code below this line
function updateRecords(id, prop, value) {
if (value === "") {
  delete collection[id][prop]; 
} else if (prop === "tracks") {
  collection[id][prop] = collection[id][prop] || [];
return collection;

updateRecords(5439, "artist", "ABBA");
updateRecords(5439, "tracks", "Take a Chance on Me");
updateRecords(2548, "artist", "");
updateRecords(1245, "tracks", "Addicted to Love");
updateRecords(2468, "tracks", "Free");
updateRecords(2548, "tracks", "");
updateRecords(1245, "album", "Riptide"); 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

I have the same question! I don’t understand either. I’m guessing to make it an array if it isn’t an array, but I’m not sure. What error does it show?

That line says, in words, “if collection[id][prop] does not exist, then make it an empty array, otherwise it is itself”.

You need this because you can’t push onto an array that doesn’t exist.

One of the indications of the challange is:

If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.

So you will test if “prop” is "tracks"with:

else if (prop === "tracks")

Now you will use an interesting feature of Javascript, within an assignment you can have an ‘or’ operator, if the left value fails(0, empty array or string, false or empty object), it will assign the right one:

collection[id][prop] = collection[id][prop] || [];

For example:

let A = 0;
let B = A || 1;
console.log(B); //1

So you will assign collection[id][prop] to itself but if it doesn’t exist it will create one empty array. It is shorter than write and if-else statement.


Awesome, thanks for providing a detailed answer!