Are JavaScript Arrays Primitives? Strings? Objects

Are Javascript arrays primitives? Strings? Objects?

Arrays are objects.

However, unlike regular objects, arrays have certain special features.

  1. Arrays have an additional object in their prototype chain - namely Array.prototype. This object contains so-called Array methods which can be called on array instances. (List of methods is here: http://es5.github.com/#x15.4.4)

  2. Arrays have a length property (which is live, ergo, it auto-updates) (Read here: http://es5.github.com/#x15.4.5.2)

  3. Arrays have a special algorithm regarding defining new properties (Read here: http://es5.github.com/#x15.4.5.1). If you set a new property to an array and that property's name is a sting which can be coerced to an integer number (like '1', '2', '3', etc.) then the special algorithm applies (it is defined on p. 123 in the spec)

Other than these 3 things, arrays are just like regular objects.

Read about arrays in the spec: http://es5.github.com/#x15.4

Are arrays objects in JavaScript?

Arrays are indeed a type of object.

console.log([] instanceof Object);


Related Topics



Leave a reply



Submit