Trouble understanding when to use braces or brackets

Tell us what’s happening: Hello, i understand objects int JS, but im a bit confused when i should use an array[ ]
like in this excercise I’m doing right now, and when I should use braces{} like in this excercise

Your code so far

// Setup
var contacts = [
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
    "firstName": "Sherlock",
    "lastName": "Holmes",
    "number": "0487345643",
    "likes": ["Intriguing Cases", "Violin"]
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["JavaScript", "Gaming", "Foxes"]

function lookUpProfile(name, prop){
// Only change code below this line

// Only change code above this line

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Profile Lookup

Link to the challenge:


Contacts is an array with 4 object in. For example is you want the 2nd object firstName you should write something like that contacts[1].firstName.

The { and } indicate the value is an object. The [ and the ] indicate the value is an array. You use whatever syntax is needed to accomplish the task.

In the linked exercise (Record Collection), collection is an object. It has 4 defined properties:

  • 2548 is an object that contains three properties (albumTitle, artist, and tracks). The first two properties have strings for values. The third property (tracks) is an array of string elements.

  • 2468 is an object that contains three properties (albumTitle, artist, and tracks). The first two properties have strings for values. The third property (tracks) is an array of string elements.

  • 1245 is an object that contains two properties (artist and tracks). The first property has a string for a value. The second property (tracks) is an empty array.

  • 5439 is an object that contains one property (albumTitle) that has a string for its value.

Like @Undefineid said above, you also use square brackets [ and ] when you want to reference an object property’s value specifying the property key name in quotes or without quotes if using a variable name.

collection['1245']['artist] // "Robert Palmer"
var id = 2468;
collection[id][albumTitle] // "1999"
1 Like

Is it possible to achieve the same things with { } and [ ], because i cant understand the difference .

That depends on what you are trying to achieve. Do you have an example?

No, but the difference seems nonexistent for me. If you have the time to explain it to me, I would be happy!

It might be easier if you posted some code and asked what you don’t understand or what you can not tell the difference in.

Ok, the excercise i posted above uses [ ], why doesnt it use { }?

I’m sorry if my questions are stupid.

So here’s a thing - both array members and object properties are accessible by using square brackets. It can get confusing, but there’s a little trick you can use to tell which is which.

It isn’t the brackets that tell you which you’re accessing… But what’s in the brackets. If the value itself is a number, like members[3], then you’re accessing an array, and the fourth value in that array.

If, on the other hand, the value in the brackets is a string value, then you’re accessing an object property, like record["recorded_date"] or even car["glove box"].

Property names are strings, while array indices are numbers. But be careful - collection["2468"] is not the number, but the string value! Note the quotes.

It can become a little more challenging when you have something like collection[id][property], and you have variable names instead of actual values. Is id a string or a number? Cheat a little, console.log(id) or console.log(typeof id) can tell you what the value is, or whether it’s a string or number.

They aren’t stupid at all, they’re asking good fundamental questions and showing you are trying to understand.

On an object, we can use two formats to get to properties - collection["id"] and both access the same property. But notice, in the first, the property name is given as a string. If we want to be able to dynamically access properties, we can use a variable inside the square brackets:

let id="2468";
let record = collection[id];
// in the above, id becomes the string it contains

As to why collection[id] and not collection{id}, it’s largely convention - but also, using { ... } there tells javascript “this is a block of instructions, run them all!”

Both [ ] and { } wear many hats in javascript (and other languages). As disappointing as it is to hear, they’re something you become more able to differentiate with practice.