1

What is the difference between this

var person = {
    name: "Bob",
    age: "99"
};

and this?

var person = {
    "name": "Bob",
    "age": "99"
};

Or do they mean the same thing? If they do, what if I want the key to be an object? How would I specify the object as the key if name means "name"?

4
  • Object can't be the key. Object can be a value. Commented Jul 4, 2013 at 19:48
  • @ElmoVanKielmo that statement is both vague and misleading. The string "object" is a perfectly valid property name. Commented Jul 4, 2013 at 19:51
  • 3
    exact duplicate of What is the difference between object keys with quotes and without quotes? Commented Jul 4, 2013 at 19:55
  • @MattBall, do I say string "object" is not a valid property name? I don't even say x = {object: "foo"}; is invalid! It is valid but the key object of x is converted implicitly to string "object" and has nothing to do with object from global namespace. Misleading? Yes, using keys like object or "object" is misleading, confusing etc. And don't try to tell me that string is an object too. I know that. The main thing is that whatever the key is, it will be converted to string if it isn't string already. No way to have "object key" with custom methods and attrs. Commented Jul 5, 2013 at 7:17

3 Answers 3

3

There is no difference. Quotes are only necessary if you want to use a string as a property name, but that string is not a valid identifier. Further,

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has space or dash, or starts with a number) can only be accessed using the square bracket notation.

(source)

Object literal syntax is covered in-depth on MDN.

Sign up to request clarification or add additional context in comments.

7 Comments

Another good link I found with helpful explanations. link
Indeed. Mathias knows his stuff.
and on most browsers, quotes are necessary if the property name is a keyword
@Alnitak keywords are a subset of strings which are not valid identifiers, but it can't hurt to be explicit.
@MattBall, keywords are not a subset of strings which are not valid identifiers. First of all, foo bar is a string and is not a valid identifier and is not a keyword in any programming language I know. Furthermore, term string in programming is a data type or/and class, in some languages equivalent of array of chars. Keywords are a subset of words or tokens - they are called reserved words too as they have special meaning in language grammar. But you can't even say that keyword can't be identifier - this is a keyword and an identifier as well in javascript.
|
0

They are equivalent in this case, but the quoted version allows you to use keys that are not valid JS identifiers. For example, this does not work:

{ -test: 42 }

while this does:

{ "-test": 42 }

You cannot specify an object as the key no matter what.

6 Comments

@akonsu: console.log(h) to see what happened. Not what you thought.
You can specify anything as a key, but it will be converted to a string first.
it gives this: Object {[object Object]: 1}
@akonsu: console.log("[object Object]") will print 1. As you see, the key is not really an object.
@MattBall: Personally I appreciate that. Beginners usually don't. ;-)
|
0

They mean the same thing. Valid keys are identifiers, string literals, or numeric literals. See http://ecma-international.org/ecma-262/5.1/#sec-11.1.5

You can't yet use objects themselves as keys, but WeakMap objects as proposed for EcmaScript 6 will solve this. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.