A detailed Code description about when to use bracket notation and dot notation to access property

//Description of bracet notation[ ] and dot notation(  .   )
// Setup
const testObj//fathervar
 = {
  12: "Namath",
  46: "Montana",
  19: "Unitas",//property
  "acd": "Abdullah" //i changed haha
};

// Only change code below this line
const playerNumber = 46;  // Change this line  (child var  to get property)
const player = testObj[playerNumber]; //grandchild var to show property detail
console.log(player) 
 

console.log("learning for fun");
 let acd=45 //practice var
   console.log(acd); //show on console 45
  const play = testObj.acd; //.acd works because it was string
  console.log(play); //show on console Abdullah
   // Change this line
 const players = testObj.playerNumber; //undefined variable
console.log(players);}//is undefined if numeric and we use testobj.var only applicale to string without spaces 
//

Hey @Abdullah404, you can learn all about property accessor here.

But for a quick explanation you use the dot notation when you are accessing a property by its identifier.
A bracket when you have a variable.

So for example

const object = {
  mark:  "Hi I am Mark"
}

object.mark // access with the identifier

const someValue = "mark"
object[someValue] // same as object['mark']

Hope this helps :smile:

2 Likes