What is the difference between the two lines

var collection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  }
};
function updateRecords(object, id, prop, value) {
if(prop!="tracks" && value!="")
  {
         object.id.prop=value; //**This line is giving error**
       object.id={prop:value};//**This is working fine**
}
  return object;
}

updateRecords(collection, 2548, 'artist', 'ABBA');
  1. Please format your code so it’s easier to read.

  2. Which two lines? What “difference” are you concerned about?

Welcome, alia.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


For future posts, if you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

To answer your question, here are the relevant lessons in the Basic JavaScript section:

I suggest you take a quick refresher of these, and come back, if you have any questions.

Hope this helps

1 Like

I agree with Sky. But looking at your comments:

object.id.prop=value; //**This line is giving error**
object.id={prop:value}; //**This is working fine**

It’s because object.id is not defined. In the first line, when it tries to evaluate object.id.prop, it is looking for the property “prop” on undefined and it will throw an error for that. In the second line, object.id is still undefined, but that’s OK because we are not looking for a property on the undefined but overwriting it with a new object.

To put it another way, if you put before these lines:

console.log(typeof object.id);

it should return “undefined”.

Furthermore (as an experiment) you could make the first line work by preceding it with:

object.id = {};

so it is an actual object on which it is looking for a property.

1 Like