Using Objects for Lookups - quotes in object properties

I have solved the challenge, but I am puzzled about the difference between quoted and unquoted object properties. I have been looking for clear, straightforward explanations, but haven’t found any. Could anyone help? When is it necessary to use quotes in object properties?

var myDog {
  "tails": 1,
}

vs

var myDog {
  tails: 1,
}

Thanks!

Your code so far

// Setup
function phoneticLookup(val) {
  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("charlie");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

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

In the cases above, the quotes are not necessary. In some cases they may be. You can have a property name with a space in it (but it’s bad practice).

const someObject = {
  "some property": "I have spaces"
}

It’s also possible that the name of a property could happen to be the same as a variable (again, try to avoid this).

let coolestPerson = "ArielLeslie";
const forumMods = {
  mostHelpful: "JacksonBates",
  coolestPerson: "ArielLeslie" // you should have used quotes here.
}
// forumMods has 2 properties: "mostHelpful" (JacksonBates) and "ArielLeslie" (ArielLeslie).
1 Like

Upon testing, I disagree that using unquoted strings as keys when they are already an identifier for a variable will cause issues.

When I executed your code, I got the following object

{mostHelpful: "JacksonBates", coolestPerson: "ArielLeslie"}

and ArieLeslie never appeared as a key. :slight_smile:

1 Like

I didn’t test it and must have gotten my language quirks mixed up. Excellent science-ing.

Thanks, ArielLeslie. The “name with a space” issue, seems helpful to understand a clear case for using quotes (although I also understand the advice of preferably not using spaces). But part of my question remains: Do the quotes function just as a grouping/boundary marking for the keys/properties? Or do they change somehow the quality or possible uses of the keys/properties? So, in your example, would mostHelpful be exactly the same as “mostHelpful”?
Thanks again! Sorry if this is a very basic question!

Yep, both of them are the same when the key doesn’t begin with a number and is entirely alphanumeric (plus a few extra characters: _, $, etc.)

2 Likes