Is there any difference between:
NewObject = {'foo': 'bar'}
And
NewObject = {foo: 'bar'}
For they seem to be working in the same way.
Is there any difference between:
NewObject = {'foo': 'bar'}
And
NewObject = {foo: 'bar'}
For they seem to be working in the same way.
No difference. Using quotes is required if the key name is a reserved word or contains special characters:
{ 'foo+2' : 'bar' }
{ 'finally': 'foo' }
Otherwise no quotes required.
[] syntax, for example: var NewObject = {}; NewObject[foo] = 'bar'; <-- this will use the foo variable rather than the "foo"-string. The notation NewObject.foo is equivalent to NewObject['foo']. It's up to you which one you prefer to use.JSON.stringify-ing your variable will add quotes automatically (not talking about special characters here), but if you create it manually using unquoted properties it'll result in an Invalid JSON. {name : "foo"} // invalid JSONJavaScript does not have dynamic variables but you can create dynamic properties. For example:
arr['a' + 3] = 4;
console.log(arr.a3); //4
The answer is that there is not difference.
In this case the appropriate way to do it is by using double quotes {"a3": 4} because that is the syntax for JavaScript object notation.