Trouble with when to use brackets and when dots

I have two codes taken from FreeCodeCamp tutorials and I don’t get why in one of them I need to use brackets and in the other one, I don’t.

First code:

There, we need to use brackets on [user], if you use dot it won’t work. The explanation I found here in Forums, is that user is not a variable (variables are the ones that can be accessed by dots), since it’s an object we need the brackets. Or at least that is what I understood…

But, then I have this other code, which as I can see there is an object inside an object , and it’s being accessed only with dots and it works perfectly… why is that?

Going crazy here! Need a little help hahaha :smiley:

On a Friday night lols.

1 Like

So, jumping back to your first example, we have a users object, which contains a collection of sub-objects. Each of those sub-objects have property names like Alan or Sarah, right? None of them are named user.

Instead, inside that block of code that goes:

for (let user in obj){ // we're passing in users as obj, so see that as 'for user in users'
  if(obj[user].online){ // here, we're using one of the keys from the users object: 
                                     //'Alan','Jeff','Sara', or 'Ryan' are each properties of Users, and each,
                                     //  in turn, will be assigned to the user variable. So, on the first pass,
                                     // user=='Alan'. Read that line as:
                                     // if (obj["Alan"].online)

so, inside that for statement, we create a variable that we’re calling user. That variable will be assigned, in turn, to each key of our object (in this case, the users object). Those keys are the names of the sub-objects. We could check each manually, like, but we don’t know how many users there might be. Instead, by taking the property names in that for loop, we can use those property names as a variable.

Whenever we want to use a variable to get a property, that variable is likely going to be a string or a number - it might be user == 'Alan' or index == array.length-1. Both of those are variables, which we can use to get to a specific property or array member:

let user == 'Alan';
users[user].online == users['Alan'].online == // all the same thing.

IN THAT SAME EXAMPLE, we are using dot notation too! See that .online on there? We know the name of the property we want to get, and that isn’t changing - so it’s not a variable, it’s a property name.

In the second example, we can use dot notation rather than brackets, because it won’t be changing. We could, if we wanted, go userObj['data']['friends'], it references the same property. But using brackets means we’re using a string to get the property name, while using dots means we’re simply using the property name directly.

1 Like
const myObj = {
    name: "Tom"
} // returns Tom

function getValue(value){
    myObj.value // returns undefined
    return myObj[value] 

getValue('name') // returns Tom

thank you a lott!!! I get it now!!! :sunglasses:

1 Like