ismail baig
ismail baig

Reputation: 891

In java script whats the difference between var a = xyz[] and var a = xyz{}

In java script (or jquery) , what is difference between

var a = xyz[] 
var a = xyz{}

I searched on internet but couldn't find any answer? please let me know if there is any difference.

Upvotes: 0

Views: 802

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27282

That's invalid syntax.

var a = [];

Initializes an array instance.

var a = {};

Initializes an object instance.

The syntax you describe is invalid. However, if you have an array a, you can access elements of it thusly:

a[0]

And if you have an object a, you can access properties of it the same way:

a['propName']

You can also access properties with the dot notation:

a.propName

But if your property contains invalid identifier characters (anything other than a-z, A-Z, $, and _), you must use the bracket notation:

a['prop name with ## stuff']

Note that arrays can have properties and objects can have numeric property names. This is because arrays are objects in JavaScript, albeit special ones that handle numeric properties differently than objects, and have some functionality built in (as well as a length) property.

Upvotes: 8

Related Questions