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:

Hi,

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.

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

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

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 collection.id 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.