-1
  const lookup = {
    alpha : 'Adams',
    bravo : 'Boston',
    charlie : 'Chicago',
    delta : 'Denver',
    echo : 'Easy',
    foxtrot : 'Frank'
  }
  
result = lookup[val];

im confused as to why i am able to do lookup[val] but lookup.val would not work here.

or something like

const testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};
const playerNumber = 16;  
const player = testObj[playerNumber];

testObj[playerNumber] would work but testObj.playerNumber would not. i initally assumed because 12,16,19 were int but even changing them to strings would be the same result where dot notation dont work but bracket will.

when storing an objects property into a var are we only allowed to use bracket? is so, how come if i was to do console.log(testObj.playerNumber) would not work but console.log(testObj[playerNumber]) will?

Andrew Lin
  • 15
  • 5
  • 1
    When you run this code. You will have self descriptive error message like `lookup[echo]` then you will get `ReferenceError: echo is not defined`. From this error you will come to know that JS is looking for `echo`. Instead you should use `lookup['echo']` or `lookup.echo` – DecPK Oct 04 '22 at 02:30

1 Answers1

1

By doing testObj.playerNumber you are not invoking the variable playerNumber, but rather accessing a playerNumber property within the testObj object.

Something like this:

const testObj = {
  playerNumber: "12"
};

testObj.playerNumber // 12
Brian
  • 309
  • 2
  • 10