Why i cant use dot operator instead of [ ] while assigning result variable

Why i cant use dot operator instead of [ ] while assigning result variable
0

#1

Tell us what’s happening:

Your code so far


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

  // Only change code below this line
  var link = {
    "alpha":"Adams",
    "bravo":"Boston",
    "charlie":"Chicago",
    "delta" : "Denver",
    "echo":"Easy",
    "foxtrot":"Frank"};
    
    result= link.val;
console.log(result);
  // Only change code above this line
  return result;
  
}

// Change this value to test
phoneticLookup("charlie");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups


#2

link does not have a property val.

console.log(link[val]); //works
console.log(link["alpha"]); // works
console.log(link.alpha);  //works
console.log(link.val); // does not work

#3

This (the object[] syntax) means “evaluate what is in the brackets”:

link[val]

Object keys are strings, so JS will try to evaluate to a string.

So in phoneticLookup("charlie");, val is "charlie", so link[val] is going to evaluate to link["charlie"] which is the same as link.charlie. Notice how link.charlie is the last step.

link.val

Is saying look for a key named “val” on the object link. There isn’t a key called "val", as @alhazen1 says