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 string property keys "0", "1", "2", etc.

[see JS: Object Overview]

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

Array is Object

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

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

console.log ( Reflect.ownKeys ( aa ) ); // [ '0', '1', 'length', 'xx' ]

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

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

Index vs Property Key

The index of array is just string property key.

const rr = [3,4];
console.log( rr.hasOwnProperty("0") ); // true
console.log( rr.hasOwnProperty("1") ); // true

[see JS: Property Dot Notation / Bracket Notation]

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 results 「undefined」
const arr = [3];
console.log(arr[200] === undefined); // true

Check If a Object is Array

Call Array.isArray(value)

[see JS: Array.isArray]

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' ]

[see JS: Array.prototype.length]

Array Methods

Common array operations should be done using array methods.

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]

For more array methods, see:

JS: Array.prototype

String to Array

str.split(…) → split string and return a array.

[see JS: String.prototype.split]

Max Number of Elements

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

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 is treated as a property key.

const hh = ["a"];
hh[1] = "b";
hh[2**32] = "c";

console.log ( 2**32 === 4294967296 ); // true

console.log(hh); // [ 'a', 'b', '4294967296': 'c' ]
// note, the c is printed differently

); // [ '0', '1', 'length', '4294967296' ]
// all are property keys

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

JS Array

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

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials