JavaScript Arrays: The Difference Between [] and { }

By  on  

Using Moo 1.2 has taught me a lot about JavaScript. Of course, that means I've made a lot of mistakes but that seems to be the best way for me to learn. While browsing the Moo source, I'd always wondered the difference between arrays using brackets ([]) and braces ({}). Here's what I learned.

Use brackets for an array of simple values.

//examples
var answers = ['yes','no','maybe'];
var names = ['David','Kristina','Charlie','Angela'];

Use braces for key => value arrays and objects/properties.

//example - random array
var programmer = { 'name':'David Walsh', 'url':'https://davidwalsh.name', 'girl':'Kristina'}

//example - used for an object's properties
var Element.implement({
getText: function(){
return this.get('text');
}
});

This is similar to PHP's array system.

$arr = array('name'=>'David','position'=>'Programmer');

Have anything to add? Please share!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    MooTools CountDown Plugin

    There are numerous websites around the internet, RapidShare for example, that make you wait an allotted amount of time before presenting you with your reward. Using MooTools, I've created a CountDown plugin that allows you to easily implement a similar system. The MooTools JavaScript The CountDown class...

  • By
    Create Your Own Dijit CSS Theme with LESS CSS

    The Dojo Toolkit seems to just get better and better.  One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes.  The move to using LESS is a brilliant one because it makes creating your own Dijit theme...

Discussion

  1. This is interesting. So if you use []’s in a key=>value array it just won’t work or it’s simply improper usage?

    I must admit, I’ve wondered this myself with the MOO as they seem to have pretty creative looking code conventions (that really make a lot of sense).

  2. @Tim: That’s what I get from it. Please let me know if I’m wrong.

  3. Rasmus

    I feel like you’re missing one of the finer points in this bracket/brace post; braces define Objects – not Arrays!

    The following are essentially the same:

    var myArray = [];
    var myArray = new Array();
    

    … as are:

    var myObject = {};
    var myObject = new Object();
    

    Javascript is by no means perfect – so you’ll have to know the pitfalls, like erroneously mixing keys and indexes in an Array (like you can do in PHP – bad).
    As javascript doesn’t have associative arrays Objects are the closest thing.

    • Ahh!! I am so enlightened by that! Thank you! That makes so much sense now! Too many exclamations!! I need to stop but can’t!

      On a serious note, that does clarify a few questions I had.

  4. Thanks David

    U solved my problem. I am first time using Javascript in my website and i was getting one problem with one line.

     mycarousel_itemList[counter]=  
    [{url: "ID,'thumbnail', true); ?>",link: "",title: ""}]
    ;
    

    and u solved it … thanks again…regards

  5. RD

    Thanks Rasmus. This is what Exactly i was looking for.

  6. Richard

    I’m not new to JavaScript but trying to move forward, what seems to be a mistery to me is the use of punctuation ( ‘.,’:([{‘ )???. and the rules they surely must adhere too. Not one tutorial (and I have explored a lot) explains what these things actually mean and where they are to be used. It almost seems arbitrary, like learning the bizarre ‘illogical’ rules of a spoken language. Can yo help?

    Regards,
    Richard.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!