Basic JavaScript - Using Objects for Lookups

Tell us what’s happening:

My code works but when I do result= lookup.val; dot notation, it doesn’t work. I wonder why? As I know u can use dot or bracket notation to get value

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;
}

phoneticLookup("charlie");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Using Objects for Lookups

2 Likes

This one was very astonishing for me to learn. You see dot notation . only deals with static keys. That is keys aren’t programmatically assigned. Right now the lookup is trying to find a value called “val” in the object, that simply doesn’t exist.

Bracket notation however doesn’t have this problem. It can have literally any key during runtime.

Source: Dot Notation vs Bracket Notation for Object Properties – What's the Difference?

Happy learning. :slight_smile:

3 Likes

lookup.val is equivalent to lookup["val"]

The lookup object doesn’t have a key named “val”.

2 Likes

Oh I thought . can be also used to assign a parameter. so it is only [] when u r trying to assign a parameter

2 Likes

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