Can we solve this program using Dot notation. I tried but i found undefine of this result

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Setup
function phoneticLookup(val) {
let result = "";
const lookup = {
  "alpha": "Adams",
  "bravo": "Boston",
  "charlie": "Chicago",
  "delta": "Denver",
  "echo": "Easy",
  "foxtrot": "Frank"
}
console.log(lookup.val);
// Only change code above this line
return result;
}

phoneticLookup("charlie");
  **Your browser information:**

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

Challenge: Using Objects for Lookups

Link to the challenge:

lookup.val

Is the same as

lookup["val"]

Is there a property with the key "val" on that object?

Can we solve this program using Dot notation

No, because dot notation is not dynamic, and this program requires dynamic lookup.

Could you explain more?

lookup.val

This is will always evaluate to the property literally named “val”.

lookup[val]

This will first evaluate val, and then lookup the property with the matching name. So if val is "name", then the “name” property will be evaluated. It’s dynamic based on the value of the val variable.

1 Like

Here, let me show you with some code.

const lookup = {
  alpha: "Adams",
  bravo: "Boston",
  charlie: "Chicago",
  val: "Nope",
};

let val = "alpha";
console.log(lookup.val);  // Nope
console.log(lookup[val]); // Adams

val = "bravo";
console.log(lookup.val);  // Nope
console.log(lookup[val]); // Boston

val = "charlie";
console.log(lookup.val);  // Nope
console.log(lookup[val]); // Chicago

val = "val";
console.log(lookup.val);  // Nope
console.log(lookup[val]); // Nope

Dot notation is not dynamic.

oh great man i got it .
Thanks a lots.