What is the difference between object keys with quotes and without quotes?

json key without quotes
javascript object key with special characters
property keys must be double quoted json
json object without double quotes
add double quotes to object keys
javascript object key string
key/value pair javascript
javascript string without quotes

Is there any difference between

obj = {'foo': 'bar'} 

and

obj = {foo: 'bar'}

I have noticed that you can't use - in the key when I don't use the quotes. But does it actually make a difference? If yes, which?

No, the quotes do not make a difference (unless, as you noted, you want to use a key that’s not a valid JavaScript identifier).

As a side note, the JSON data exchange format does require double quotes around identifiers (and does not allow single quotes).

Unquoted property names / object keys in JavaScript · Mathias Bynens, Often they find it difficult to distinguish JavaScript Object and JSON data. So, if there's spacing in the key name, quotes are used. i.e. if, else, for cannot be used as JavaScript object Keys without single/double quotes too! Python string functions are very popular. There are two ways to represent strings in python. String is enclosed either with single quotes or double quotes. Both the ways (single or double quotes) are correct depending upon the requirement.

From Unquoted property names / object keys in JavaScript, my write-up on the subject:

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

[…]

Bracket notation can safely be used for all property names.

[…]

Dot notation can only be used when the property name is a valid identifier name.

Note that reserved words are allowed to be used as unquoted property names in ES5. However, for backwards compatibility with ES3, I’d suggest quoting them anyway.

I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.

Never confuse JSON and JavaScript Object ever again!, In pure Javascript there is no difference. Both objects are the In JavaScript files, object keys may or may not have quotes around them. In JSON, it is required  Unless an object key is a numeric literal or a valid identifier name, you need to quote it to avoid a syntax error from being thrown. In other words, quotes can only be omitted if the property name is a numeric literal or a valid identifier name. Of course, if the property name is a string literal, it’s already quoted by definition.

There is no difference here. Just a matter of style. One of the reasons for doing this is being able to use 'super' or 'class' as a key since those are reserved keywords.

Some people might be tempted to pass in a string with whitespace then call o['I can have whitespace'] But I would call that bad practice.

What is the difference between these object literals in JavaScript , Or is the rule of thumb always to write your JavaScript objects without quotations around the keys as Andrew suggests in the video? There are two types of quotes that can be used in PowerShell: single and double quotes. Some critical differences between the two can make or break a script. Knowing these differences will make you a more effective PowerShell scriptwriter and help you avoid a rather simple mistake.

No, not to javascript. However, some JSON parsers will fail when the quotes around the keys are not present.

Object key quotations: best practice?, . This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"} . An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). In ANSI SQL, double quotes quote object names (e.g. tables) which allows them to contain characters not otherwise permitted, or be the same as reserved words (Avoid this, really). Single quotes are for strings.

There are some situations where they are different. For example, if you are using jQuery, and you are making a list of parameters to pass when calling the jQuery $() command to create an element, quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object. See jQuery(), near the bottom:

While the second argument is convenient, its flexibility can lead to unintended consequences (e.g. $( "<input>", {size: "4"} ) calling the .size() method instead of setting the size attribute). The previous code block could thus be written instead as:

Do the JSON keys have to be surrounded by quotes?, , is typecasted into a string via the toString method. Issue 2 - ast & json.dumps: If a JSON is both single quoted and contains a string in at least one value, which in turn contains single quotes, the only simple yet practical solution I have found is applying both: In the following example we assume line is the incoming JSON string object :

Is there any way to use a numeric type as an object key?, What is the difference between object keys with quotes and without , From Unquoted property names / object keys in JavaScript, my write-up on the subject:​  For example, a key can have the bitting 2, 4, 5, 2, 6. The difference between 2 and 4 is 2; the difference between 4 and 5 is 1; the difference between 5 and 2 is 3; the difference between 2 and 6 is 4. If the 4th groove had been a one, then the difference would have been 5 between grove 4 and 5. That would have violated MACS.

JSON Syntax, Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between  Explore 71 Cowardice Quotes by authors including Martin Luther King, Jr., Confucius, and Mark Twain at BrainyQuote.

Javascript Object Attribute With/Without quotes, The difference is that if one element in an object has quotes, then all of them must​. So there's almost no reason unquote such keys, is there? Knowledge Wisdom Quotes. “Knowledge without application is simply knowledge. Applying the knowledge to one’s life is wisdom — and that is the ultimate

Comments
  • Actually, the quotes can make a difference if you use a numeric literal as a property name. For example, obj = { 12e34: true }; is not the same as obj = { '12e34': true };. The former would require you to access the property through obj['1.2e+35'], while for the latter you’d use obj['12e34']. See my answer for more details.
  • True but it would be brilliant if double quotes were optional given a-z first character + alpha-numeric and may quite some whitespace fans aka invisible spaces or tabs? great for data not deeply nested.
  • This tool was a life saver. Thanks for posting this.
  • I was on my way off a cliff, about to fall down a very steep incline, pummeling away towards a certain, utter, bloody death; but alas, I clicked a link in your answer, which had a very simple JS tool in it, which prevented me from falling off that cliff. This tool was a life saver.
  • Keys without quotes are invalid in JSON.
  • I don't think that example says what you think it says. The issue you've quoted is because jQuery has a method called size, and the conflict between the method size and the attribute size is resolved by selecting the method. There are cases in jQuery where passing a Value as a string or another type causes a change in behavior, but never from defining a property with a valid property name in quotes vs not in quotes.