Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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