Notations for accessing objects

Notations for accessing objects
0

#1

Hello, I’d like to ask why I’m failing this challenge when I’m using dot notation, when I change it to bracket notation it passes without any problems.
Code I’m reffering to:

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

  // Only change code below this line

  var testVar = {
    alpha : 'Adams',
    bravo : 'Boston',
    charlie : 'Chicago',
    delta : 'Denver',
    echo : 'Easy',
    foxtrot : 'Frank'
  }
  var result = testVar.val;
  
  // Only change code above this line
  return result;
}

// Change this value to test
console.log(phoneticLookup("alpha"));

#2

Because you’re not looking for testVar.val, you’re looking for testVar.alpha. By placing val into brackets, it is taken as a variable, rather than a property name.


#3

Remember the difference between bracket and dot notations. The contents of brackets are evaluated before the lookup is completed, so if you have a variable you want to put the variable name in brackets so the property looked up is that variable’s value. When you use dot notation, you are attempting to lookup a property with a name matching the dot-ed property as a literal string.


Using Objects for Lookups Dot Notation vs Bracket Notation