6

I am a JavaScript learner and want to know what is the difference between

    var obj1 = {
        attribute1 : 1
    };
    
    var obj2 = {
        "attribute1" : 1
    };
    
    console.log(obj1.attribute1);
    console.log(obj2.attribute1);

Both of them prints 1. Is there a major difference among them?

Elias
  • 3,592
  • 2
  • 19
  • 42
I.K.
  • 414
  • 6
  • 18
  • the second version would rather be how json is expecting the property label to be set, there are not great differences based on your current example, object literals one would code as the first version – Icepickle Oct 02 '18 at 10:41
  • I don't think there is a difference. Just two different coding styles. – Elias Oct 02 '18 at 10:43
  • It's not really about styles or JSON… the quotes are *generally optional* but *required sometimes*, depending on what your key is called. JSON standardises this to make it *always required*. – deceze Oct 02 '18 at 10:46
  • And JSON is about serializing, don't forget it's *not* the same thing as JavaScript Objects. – Pac0 Oct 02 '18 at 10:51

3 Answers3

5

They are equivalent in your case.

Internally, they are the same.

What could change is the way you can access them with your code.

When using strings (quoted properties), you can actually use more exotic names for your properties :

var obj3 = {
    "attribute with space": 1,
    "123AttributeStartingWithANumber": 1,
}

In my example, you cannot access those attribute names via the obj1.attributeName syntax (but you can with the brackets notations : obj1["attribute with space"] or obj1["123AttributeStartingWithANumber"] .

This is because "attribute with space" or "123Attribute" are not valid identifiers in JS.

Note that in your example you can also use the bracket notation :

console.log(obj1["attribute1"]);
console.log(obj2["attribute1"]);

In summary, to quote deceze's comment :

Quoted properties and bracket notation always work, unquoted properties and . dot access is shorthand for when your property name is a valid identifier.

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • To simplify this answer: quoted properties and bracket notation *always* work, unquoted properties and `.` dot access is shorthand for when your property name is a *valid identifier*. – deceze Oct 02 '18 at 10:53
  • Thanks, I was missing the word "identifier". I took the liberty of including your summary as quote. – Pac0 Oct 02 '18 at 11:16
1

There is no difference.

Object literal syntax allows you to use a string or an identifier to provide the property name.

A string allows you to use characters (such as or .) which are not allowed in an identifier, but attribute1 has none of those characters in it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You can make object keys with space in them if you declare them as strings:

var obj1 = {
   attribute1 : 1 // but you cannot say my attribute1: 1 it will throw syntax error
};

var obj2 = {
    "my attribute1" : 1 
};

console.log(obj1.attribute1);
console.log(obj2["my attribute1"]);
Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46