JavaScript

jQuery/Javascript variable arrays, brackets [ ] or braces { }?

Hi! I'm at Andrew's Responsive Slider part for jQuery. At one point of the tutorial he said to create a new variable but he used braces { } instead of brackets [ ].

The code is as follows:

var options =  {navigationControl: 'false', animation: 'slide'};

From the earlier Javascript lessons I had learned to create arrays you need to use brackets [ ]. So what is the difference between braces { } and brackets [ ] in creating arrays? Which should I use during which occasions? Are the ones above called Associative Arrays, does that mean these arrays exist already and are just being referred to using braces { }?

As you can see, I'm pretty much the noobest noob at this lol. Thanks for taking time to answer!

1 Answer

Good question! Being a Rubyist, I was surprised to find that there is no actual Hash / Associative Array class in Javascript. What looks like an hash is acutally an Object. But it works just like a Hash:

var stuff = {'tree': 'plant', 'tiger': 'feline'}
stuff['tree']
--> "plant"
stuff['tiger']
--> "feline"

Here's some more info: http://www.quirksmode.org/js/associative.html