Using Objects for Lookups: Do I have to made empty var Result?

Tell us what’s happening:
Question: Do I have to made empty var Result?
Even though I got rid of 'VAR RESULT = “”, it works… Why is that?

Your code so far

// Setup
function phoneticLookup(val) {
  // --Question: Do I have to made empty var Result? Even though I got rid of line5, it works.. Why is that?
  //  var result = "";

  // Only change code below this line

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/using-objects-for-lookups

It works, but it has the ugly side-effect of declaring the result variable in the global scope. Unintentional global scope declarations can cause hard-to-track bugs.

1 Like