Possible Duplicate:
JavaScript arrays braces vs brackets
I have a simple question that I can't find an answer to with Google. What is the difference between
var foo = { };
and
var bar = [ ];
An example would help.
Possible Duplicate:
JavaScript arrays braces vs brackets
I have a simple question that I can't find an answer to with Google. What is the difference between
var foo = { };
and
var bar = [ ];
An example would help.
foo = {}
is not an array, but an object (created using object literals, {}
).
bar = []
is an array, which inherit various methods from the Array constructor. An array also has various properties, such as .length
.
EDIT (regarding comment):
The a
property of an object can be accessed through foo.a
or foo["a"]
.
Looping through the properties of the object should be done using a for( .. in .. )
loop.
var foo = {"a":true, "b":false};
for(var prop_name in foo){
alert(prop_name + " - " + foo[prop_name]);
}
This code will show two alert messages:
a - true
b - false