View Single Post
Old 01-28-2013, 03:53 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];
Might be better to ask in what ways they are the same. To which the answer is simpler: In no way.

{ } is used to introduce a set of name/value pairs.

As in:
Code:
var sounds = {
    dog : "bark",
    cat : "meow",
    cow : "moo",
    "bald eagle" : "squawk!"
};
Note that you can put quotes around then names, but you don't have to unless they violate standard JavaScript naming conventions.

Also note that the above is entirely equivalent to doing:
Code:
var sounds = new Object();
sounds["dog"] = "bark"
sounds.cat = "meow";
sounds.cow = "moo";
sounds["bald eagle"] = "squawk!";
And all of this means that no matter which way you initialize the object, you can later find the value of on object element via
Code:
    sounds.cat
or
    sounds["cat"]
and so ont.

*************

The square bracket notation is used to create an array of elements (or to declare that a variable is an empty array, if there are no values between the brackets).
Code:
var arr = [ 7, 3, 21, 911 ];
var arr2 = [ "arrays", 3.14159265, "can be", new Date(), "heterogenous" ];
var arr3 = [ ]; // empty array
Array elements are then typically retrieved by element number:
Code:
alert( arr2[1] ); // shows pi
alert( arr2[3] ); // shows time and date when variable was created
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-28-2013 at 03:57 AM..
Old Pedant is offline   Reply With Quote