JS Objects: why are quotes necessary for one-word string values, but not for one-word properties?

Tell us what’s happening:
In the solution below, I pass whether or not I have quotes around the object’s properties. But if I take the quotes off of the values, I fail. For example, if I remove the quotes from “Adams,” I get the error, “Adams is not defined.” Is there a reason that for this challenge it is okay to leave quotes off of the one-word properties, but not the values?

To take a guess: Is it perhaps because the values here are supposed to be strings, and if I remove the quotes, the value then gets understood as an undefined variable, rather than a string? whereas there is not the same sort of ambiguity when it comes to properties (i.e., a property is always assumed to just be a property)?

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:

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

Challenge: Using Objects for Lookups

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

correct
but you can’t always remove the quotes in the property name and everything’s fine
there are specific rules for when you can write a property name without quotes. It may be that those are the same for valid variable names but I am not sure.

1 Like

So you call your function with the line

phoneticLookup("charlie");

You’re passing in a string value. It’s a string. Alpha-numeric-etcetera-string. BUT… inside your function, it’s no longer the string "charlie" - it’s the value of the variable val. That parameter on your function signature is assigned in this line:

function phoneticLookup(val){

That line creates a local variable inside the scope of your function, and assigns it whatever string you pass in. You can NOT use quotes (single or double), or hyphens, or spaces in a variable name. Not allowed. Because each of those things have specific meanings inside the javascript interpreter. So variable names can contain letters, numbers, underscores, dollar signs… and yeah, that’s about it. Might be other edge cases, but that gives you the idea.

So quotes are not an option in a variable name. But, in this case, we have a variable named var, with the value "charlie". The quotes are NOT part of the value, they simply tell the JS engine that this is a string.

Our lookup object could be shown a little different, it would mean the same thing. Consider this:

var lookup = {
  "alpha": "Adams",
  "bravo": "Boston",
  "charlie": "Chicago",
  "delta": "Denver",
  "echo": "Easy",
  "foxtrot": "Frank"
};

That is exactly the same as the one in the lesson. But-but-but I hear you say, as though you were a motorboat, “But the variable names in that are wrapped in quotes, and you just said I can’t DO that!!” Well, no. Those aren’t variable names. They’re property names. We could just as easily have said:

lookup.charlie = "Chicago";
// or this works...
lookup["charlie"] = "Chicago";

Those are functionally exactly the same. Properties of an object are accessed by using either dot notation or bracket notation. The first line above uses the dot notation, and would have to be coded exactly into your script.

The second, however… with lookup["charlie"], we can do something. That string value inside the brackets could be a hard-wired string, as we’ve done, or we can use a variable that contains a string: lookup[var]. Remember, var == "charlie", so we’re saying “use the value of var inside these brackets, where normally we’d find a string.”

I just did a little research, to determine what exactly defines a valid variable name: it cannot be a javascript keyword, it must start with one of: [$,_, an upper or lower case letter], and can contain (after that first letter) any of those values plus a whole host of other valid ASCII characters. Again, not the reserved language-specific characters (your variable name cannot contain a period, or a left bracket, that will murph the interpreter). Simply google “valid javascript variable names” to read more about that.

BUT, those same rules don’t apply to property names. As you’ll learn later, you can have more leeway in property names, but it’s not always wise.

1 Like

Thanks for confirming!

1 Like

Thanks for the thoughts, snowmonkey! It’s helpful to think about all this in the wider context that you’ve laid out.

1 Like