Mixing bracket and dot notation confusion

Tell us what’s happening:
I do not understand why dot notation must be combined with bracket notation here. The user is a property of usersObj, and online is a property of user, so why can’t we access the value using either usersObj.user.online or usersObj[user][online]? Why does it have to be a combination? This seems rather arbitrary to me, so I must be missing something important here.

  **Your code so far**

function countOnline(usersObj) {
// Only change code below this line
let count = 0;
for (let user in usersObj) {
  if(usersObj[user].online == true) {
return count;
// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36.

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

They are different because user is a variable that contains a string that it an object property and “online” is an object property.



works because it will look for the property that matches the string in user and then access the property “online” off of that object.



would always try to access a property called “user”. If you have a property named that, it would work, but just for that one property.



Won’t work because you don’t have a variable online. If you put:

const online = `online`;

before it, it would work.

This would actually work:


because you are using bracket notation but are just passing it the string you want. It is functionally equivalent to:


Does that help?

Makes perfect sense now! Thanks!

And just to be clear…

Generally you will use dot notation. The two cases where you use bracket notation:

  1. The key/property name is stored in a variable.
  2. The key/property name is not a valid JS identifier, like if it has a space or a hyphen in it.

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