Basic JavaScript - Using Objects for Lookups

Tell us what’s happening:
Hi guys! For result = lookup[val]; at the end, is there a reason why only bracket notation works and not dot notation?

Your code so far

// Setup
function phoneticLookup(val) {
  let result = "";

  // Only change code below this line
 
 const lookup = {
   "alpha": "Adams",
   "bravo": "Boston",
   "charlie": "Chicago",
   "delta": "Denver",
   "echo": "Easy",
   "foxtrot": "Frank",
    }

result = lookup[val];
  // Only change code above this line
  return result;
}

console.log(phoneticLookup("charlie")); 

Your browser information:

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

Challenge: Basic JavaScript - Using Objects for Lookups

Link to the challenge:

What is the purpose of using brackets? What do they allow you to do? Maybe an example will help you answer this? What is the difference between

myObject["value"]

and

myObject[value]

Does using dot notation allow you to make this distinction? If not, which one does dot notation implement? In other words, which of the following is correct:

myObject["value"] == myObject.value

or

myObject[value] == myObject.value
1 Like

I see. Using dot notation makes it so “value” is a string while bracket defines it better. In other words myObject.value = myObject[“value”] which is not what I’m looking for. Thanks!

1 Like

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