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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
aproperty of an object can be accessed throughfoo.aorfoo["a"].Looping through the properties of the object should be done using a
for( .. in .. )loop.This code will show two alert messages: