-1

I'm currently trying to learn JavaScript. I have some experience with Java and C++. I've been trying to find a clear cut and specific explanation of accessing and creating properties in JavaScript and have been unable to find one. Some question I have about objects:

var myObj = {
  firstName: "Vinnie",
  lastName: "Glaser"
}

1.) When defining objects, sometimes people put quotations around the key and sometimes they don't. What significance does this have? If there are no quotations around the key value is it still considered a string?

2.) When accessing properties of objects, sometimes people put quotations around the key and sometimes not. What is the significance of this?

3.) What practical differences are there between myObj[key] and myObj.key? There have been a few scenarios in which one is necessary over the other but I'm not sure why.

4.)

myObj[fullName] = myObj[firstName] + " " + myObj[lastName];

Why doesn't this work?

Thank you for the help.

VinnieGee
  • 21
  • 1
  • 2
  • 1
    1. quotes are necessary if you have a key like `123`; without quotes that would be interpreted as number and produce a syntax error. 2. without quotes but inside square brackets means that what you think of as key is actually a variable storing the key 3. similarly, in `myObj[key]` the `key` part is a variable. 4. again, you'd need `myObj.fullName` or `myObj["fullName"]` (because with your code, JS is looking for a variable called `fullName` but not finding it) to find resources about this, try search for "brackets notation" and "dot notation". –  Nov 21 '19 at 00:22

1 Answers1

1

1.) When defining objects, sometimes people put quotations around the key and sometimes they don't. What significance does this have? If there are no quotations around the key value is it still considered a string?

2.) When accessing properties of objects, sometimes people put quotations around the key and sometimes not. What is the significance of this?

All valid keys are interpreted by the system as strings regardless of whether they are in quotes or not and regardless of what the key actually is. Even a key of 1 would be processed as "1" and you can see this in your browser's debugger, so it's not about making the key a string or not. It's simply about whether the key name is valid syntax without them. For example, most people would say that a property (key) name can't contain spaces:

let obj = {
  my property : "foo"
};

But, that's not true. It's just the un-quoted key names that contain a space is invalid syntax. You can do it with quotes:

let obj = {
  "my property" : "foo"
};

// Then, you access the property through array-like syntax
console.log(obj["my property"]);

3.) What practical differences are there between myObj[key] and myObj.key? There have been a few scenarios in which one is necessary over the other but I'm not sure why.

Syntax. In the above example, it would be invalid syntax to try either of these:

obj.my property
obj."my property"

But, if you access keys using array-like syntax, you would have to pass in the index and in that case strings are legal syntax:

obj["my property"]

4.) Why doesn't this work:

myObj[fullName] = myObj[firstName] + " " + myObj[lastName];

Without quotes around the index values, the system believes that what you are passing are variables, so it attempts to get the values out of the variables. It would work if the values you are passing in were declared variables that have values and those values mapped to existing keys in the object.

let myObj = {
  full : null,
  first : "Scott",
  last : "Marcus"
};

let fullName = "full";
let firstName = "first";
let lastName = "last";

myObj[fullName] = myObj[firstName] + " " + myObj[lastName];

console.log(myObj[fullName]);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • "*Keys don't have data types*" is confusing if not wrong. Certainly property keys are either strings or symbols? – Bergi Nov 21 '19 at 00:42
  • @Bergi True. My meaning what that you really can't assign or coerce a key into a data type. In fact, all object keys are always strings (even if the key was a number not placed in quotes). I'll update. – Scott Marcus Nov 21 '19 at 00:44
  • Maybe talk about "object literal keys" instead of "object keys". (Ignoring computed properties :P) – Bergi Nov 21 '19 at 00:51