Question: what does "[]" means in JS?

Hi everyone! I just completed this challenge (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection) with help from one post, but nevertheless there’s one thing remaning that I didn’t get.

if(prop === ‘tracks’ && collection[id][prop] === undefined) {
collection[id][prop] = []; //What does “[]” mean? I don’t get its purpose
** }**

Can you post more of the code you are talking about please?

Without the context I can say that [] is an empty array.

Hi welcome to FCC!
Square brackets are used for:

  • Creating array literal e.g. const arr = [1, 2, 3]
  • Accessing element of an array e.g. arr[0] // returns 1
  • Accessing an object’s property
    const  myDetails  = {
                       name:  "Jane Doe",
                       age: 100
                      }

You can access name property using the syntax myDetails['name'].
But if i have an object nested within another object, you can use double square brackets.

    const  myDetails  = {
                       name:  "Jane Doe",
                       age: 100,
                       spouse: {
                                name: 'Jane Doe',
                                age: 200
                                }
                   }

You can access spouse’s name using the syntax myDetails['spouse']['name']
You can read more about Arrays Here on W3C and objects Here

1 Like

that’s assigning an empty array to collection[id][prop]

Many thanks nibble, for your explanation!

// 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": [ 
        "1999", 
        "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) {
  if (value === "") {
    delete collection[id][prop];
  } else if (prop === "tracks") {
    collection[id][prop] = collection[id][prop] || [ ];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }
  
  return collection;
}

Yes, I agree with that. But: what’s it used for? W3 School afirms " Avoid new Array()", but can someone give an example? please!

Not really sure what you are asking here. If you want an array to push to, you have to create it first. So you make an empty array so you can push to it.

Here evenNumbers is the empty array that needs to be created so we can push to it.

const numbers = [1,2,3,4,5,6];
const evenNumbers = [];

numbers.forEach(number => {
  if(number % 2 === 0) {
    evenNumbers.push(number)
  }
})

console.log(evenNumbers); // [2, 4, 6]

[quote=“leonelmoreno, post:6, topic:374887”]
collection[id][prop] = collection[id][prop] || ; @lasjorg This is the context /quote]

Please don’t create duplicate topics for the same question. I’ve closed this one.

1 Like