3

I know there are some questions that may refer to this, but I didn't find anything related to javascript strictly.

For example this: JSON Spec - does the key have to be surrounded with quotes?

But my question is: should we use quotes when we write a Javascript Object?

I think it is more elegant to write is without quotes and also more readable, but is it OK?

For example when I use it to pass as an argument:

myFunction({
    "key": "value",
    "otherKey": 10
});


myFunction({
    key: "value",
    otherKey: 10
});

Also as I read, the second one works in every browser, unless it is a forbidden word (for, if, etc), so this wouldn't be a problem.

I think the second one looks better, but I also think it may not be a good practice..

I did this practice for years and now I am starting to think that I was wrong but I don't feel it good using quotes.. What should I do?

Community
  • 1
  • 1
Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72
  • 4
    JSON and the object literal syntax in JavaScript are different things, although they are closely related. – agconti Aug 08 '14 at 19:17
  • Some people like to use quotes to be consistent, but JavaScript does not insist on quotes unless the property name is not a valid identifier (and not a reserved word). You can use single or double quotes. – Pointy Aug 08 '14 at 19:19
  • You can enclose property keys in Javascript object if you want, or leave them out. Unless the key is not a valid variable name (e.g. "my-variable") in Javascript, it doesn't matter. In JSON however, the keys must be enclosed in quotes. – Matt Burland Aug 08 '14 at 19:19

3 Answers3

6

JavaScript does not require object literal keys to be quoted if they are valid identifiers.

All of the following object literal forms are equivalent in JavaScript (since the keys are always converted to strings by the interpreter):

var a = {foo: 1};
var b = {'foo': 1};
var c = {"foo": 1};

You must only quote the keys if they are not valid "identifiers", for example:

var nonIdentifierKeys = {
  '=)': 'special characters',
  '  ': 'whitespace',
  '42': 'numbers',
  // etc.
};

Note that JSON is derived from JavaScript object literal notation but it explicitly requires key names to be quoted (with double-quotes):

{"json":"must quote keys"}
maerics
  • 151,642
  • 46
  • 269
  • 291
5

Short Answer: JavaScript object keys do not need to be surrounded by quotes.

Because:

JSON and the object literal syntax in JavaScript are different things, although they are closely related.

JSON has to be transmitted as a string between the server and the client. Because of this, all the values need to be wrapped in "" so they can be tokenized, split, and reconstructed back into objects when they arrive.

The javascript literal syntax does not have to go through this transfer, and so the quotes are not necessary. Convention dictates that you do not use quotes unless the key has a space or is a special reserved word.

Community
  • 1
  • 1
agconti
  • 17,780
  • 15
  • 80
  • 114
  • Thanks, but is it a good practice to avoid the quotes when possible? – Pablo Matias Gomez Aug 08 '14 at 19:32
  • 1
    Its always good to avoid, unnecessary things when coding. In the case of quotes around keys, its important because of syntax highlighting. If you quote the keys, your editor will not re-color the key when its a function vs a constant, (or it will re-color it strangely), ect. Furthermore, typical themes reserve the brightest colors for stings. This would give your keys unnecessary visual precedence, because what you really care about is that keys value. – agconti Aug 08 '14 at 19:38
2

You do not have to quote keys, unless they aren't valid identifiers (e.g. they contain spaces or they are reserved words). The object literal syntax isin't the same as JSON. It's probably a simple matter of preference, but I only quote keys when required to do so.

plalx
  • 42,889
  • 6
  • 74
  • 90