Javascript Array - Vs Object


If you do not know the declaration of the object and the array beforehead, it is difficult to tell the difference between an object and an array

Take a look at the following example


<html>
<body>

<script type="text/javascript">
var x = [5,10,15];
var y = {0:5,1:10,2:15}

z = y[0] + y[1] + y[2]
document.writeln(z);  // outputs: 30

</script>

</body>
</html>



Try this example online here .


The point we want to make is this - if you looke at the line in isolation
z = y[0] + y[1] + y[2]

It is hard to tell if y is array or an object. The array and objects has some similarity and differences.
Difference

Their prototyped methods differs.
Object do not support the arrays built in length method.
Arrays auto increment their indexes. Object behave differently. More like associative Arrays (- you define the index and elements.
New Objects use the {} literal and new Arrays use the [] literal.


Similarities
Both Array and Object is has typeof as “object”.
You can store multiple values(data types) in Array and Object.