Bracket Notation vs Dot

Could anyone explain why usersObj.users.online === true doesn’t work, but usersObj[users].online does, even though both options are in solution hints. What’s the deal here ?

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

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

Link to the challenge:

const users = {OP:'ViyanMd', reply:'santimir'}
for (let user in users){
let val = users[user]; console.log(val)
}


for (let user in users){
let val = users.user; console.log(val)
}

Run in browser console or here

1 Like

Square bracket notation allows access to properties containing special characters and the selection of properties using variables.
Using dot notation you can access properties that do not contain special characters or variables.

This article might help you Dot Notation vs. Bracket Notation

1 Like

I’ve missed the fact that user was a variable. Now I see. Thanks!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like