Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between JavaScript Array and Object except .length property?

As I think, JS array is just a hash-map which accepts only integral value as a key. And the .length property just return largest index + 1.

Is this right? Is there any other differences?

like image 222
eonil Avatar asked Jul 30 '10 12:07

eonil


People also ask

What is the difference between array and object in JavaScript?

In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes.

What does .length do in JavaScript array?

The length property of an Array object represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

What does .length do in an array?

The length property sets or returns the number of elements in an array.

What is the difference between {} vs [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.


1 Answers

You are wrong; arrays can have any keys you want.

Also, they inherit the Array prototype.

like image 58
SLaks Avatar answered Sep 22 '22 16:09

SLaks