JavaScript "Record Collection" problem!

hi every one
there is a test in one of “Basic JavaScript” with name “Record Collection”.
in this (https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-record-collection/18261) topic has two solution an in solution 2 there is this code and its doing well, but when it remove barackets frome [value] in the end , it doesn’t work
do you know why?
whats the difference between [value] and value from upper elseif ?

else if (prop === "tracks" && object[id].hasOwnProperty("tracks") === false) {
    object[id][prop] = [value];

Hello, why did you remove the bracket?

I believe JavaScript needs both opening and closing bracket e.g

if(){//do something}
else if(){//do something}

i mean instead of this [value] i wrote this value
and in two line upper than that, there is a value without brackets and if you add bracket to that, you’ll get error!

Ah sorry! [value] is value within an Array. value is referring to just value.

1 Like

yes thank you
i know that
but i want to know what benefits us if we use value within an Array somewhere an use just a value somewhere else?

Hello there,

The benefit is when you want to initialise an array with one object, because you plan on adding more objects later down the line:

const myObj = { name: "ali" };
// Let us add something to myObj
const food = "apples";
myObj.favouriteFoods = food;
// Now, i want to add more foods:
myObj.favouriteFoods.push("orange"); // ERROR...cannot .push() onto a string

// Start again:
const myObj = { name: "ali" };
// Let us add something to myObj
const food = "apples";
myObj.favouriteFoods = [food];
// Another food
myObj.favouriteFoods.push("oranges"); //Success - what we wanted

Hope this helps

1 Like

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