Using FOR..IN to loop through objects

Please help me understand what is happening here:

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
for (let usersOnline in obj) {
  if (usersOnline[online] == true) {
    return true;
  }
}
return false;
  // change code above this line
}

console.log(countOnline(users));

What I want to do here is return true if Alan, Jeff, Sarah, Ryan, 's online status is true.

But the results says that online is not defined. Why is that?

Because dot notation and brackets notation are not the same, this article explains very well why ! :slightly_smiling_face:

To get your example working, just replace usersOnline[online] by usersOnline.online

2 Likes

Thank you for the article @anon38736429. exactly the answer I needed.