Root149
Root149

Reputation: 419

Difference in syntax when creating an object

Is there any difference between:

NewObject = {'foo': 'bar'}

And

NewObject = {foo: 'bar'}

For they seem to be working in the same way.

Upvotes: 0

Views: 36

Answers (2)

ariel_556
ariel_556

Reputation: 378

JavaScript 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.

Upvotes: 0

Liglo App
Liglo App

Reputation: 3819

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.

Upvotes: 2

Related Questions