JS: Understand JS Array

By Xah Lee. Date: . Last updated: .

This page is advanced understanding of JavaScript array.

For basics of JavaScript array, such creating array and access elements, see: JS: Array Basics.

JavaScript array is a object data type, with a magic property key "length", and special treatement of properties whose names are non-negative integers. [see JS: What's Object?]

JavaScript array is not a linear sequence of memory locations. Forget what you know about “array” in other languages.

By understanding array is just a special JavaScript object, you avoid lots mysterious bugs from using JavaScript array.

Array as Object

JavaScript Array is of object data type. Meaning, it is a key/value pairs. For example, we can add property to array:

// array is a object, you can add properties to it
const aa = [3,4];
aa.xx = 7;
console.log(aa); // [ 3, 4, xx: 7 ]

Array also has the attribute “extensible”, just like other JavaScript objects. [see JS: Prevent Adding Property]

console.log(
 Object.isExtensible( [3,4] )
); // true

Object methods also work on array.

// array indexes are just property keys
const ee = ["b", "c"];
console.log(Object.getOwnPropertyNames(ee)); // [ '0', '1', 'length' ]

Accessing Array with Non-Existent Index

Accessing array with non-existent index returns undefined, just like accessing a object with non-existent property.

// accessing array with non-existent index has a value of 「undefined」
const arr = [3];
console.log(arr[200] === undefined); // true

const obj = {"a":1};
console.log(obj["h"] === undefined); // true

To understand JavaScript object, see:

Check If a Object is Array

Array.isArray(value) → return true if value is a array object.

console.log(
    Array.isArray([]) // true
);

console.log(
    Array.isArray({}) // false
);

Note: You should not use typeof value, because typeof [1,2,3] returns "object".

Note: You should avoid using value instanceof Array because “instanceof” is not in general for checking on data type, and can get unexpected result for user created object. [see JS: “instanceof” Operator]

Creating Array

JS: Create Array

Sparse Array

JS: Sparse Array

Array Length Special Property

Array has a length property. It's a special property. The “length” property is automatically updated when array elements are added or removed using array methods.

Array length can be set. If you set it, the array will be lengthened or shortened.

// creating a sparse array by setting the length property

const aa = ["a", "b"];

// set the length property beyond the last index
aa.length = 3;

console.log(aa.length); // 3

console.log( Object.getOwnPropertyNames(aa)); // [ '0', '1', 'length' ]

console.log(aa); // [ 'a', 'b',  ]
// truncating a array by setting its length

const aa = ["a", "b", "c"];
console.log(aa.length); // 3

aa.length = 1;

console.log(aa.length); // 1
console.log(aa[1]);     // undefined

console.log( Object.getOwnPropertyNames(aa)); // [ '0', 'length' ]

Array Methods: Add, Remove, Extract, Elements, Join Array …

Common array operations should be done using array methods. Such as prepend, append, join, select sub-array, delete elements.

[see JS: Array.prototype]

To add/remove element(s) in middle, use splice. [see JS: Array.prototype.splice]

Warning: Never use delete to remove element in a array, because that creates a sparse array. [see JS: Sparse Array]

Split String to Array

To split a string into a array, use string method str.split(…). [see JS: String.prototype]

Array Comparison

JS: Compare Array Equality

Array-like Objects

JS: Array-Like Object

Max Number of Elements

The max number of elements is 2^32 - 1 (which is 4 294 967 295).

ECMAScript 2015 §Ordinary and Exotic Objects Behaviours#sec-array-exotic-objects

When the index is a number between 0 to 2^32-2, inclusive, it is treated as array index, else it's just a property key.

// When array index is beyond 2^32 - 2, it treated as a property key.

const hh = ["a"];
hh[1] = "b";
hh[4294967296] = "c";

console.log(hh); // [ 'a', 'b' ]

// but has a property
console.log(
    Object.getOwnPropertyNames(hh)
); // [ '0', '1', 'length', '4294967296' ]

Note: if you create a array with just 1,000,000 items, the browser will become not responsive.

Array How-To

JS: Array How-To

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth