Skip to main content

Arrays and methods

Array

Two ways of creating empty array

let arr = new Array();
let arr = [];

Create an array with elements in them

let arr = ["Apple", "Orange", "Plum"];

Array in JavaScript is heterogeneous, you can store element of any type in them.

arr.length property to get the number of element in array if you used it properly. Otherwise, if you insert item into an array with a huge gap like below

let arr = [];
arr[999] = 59;

console.log(arr.length); // 1000, it is the index of the last element + 1
arr.at()

Normally, if you index an array you can only use positive indexing, i.e. [0..length - 1]. You can use negative index with the arr.at method.

pop/push

Use pop to remove element from the end of the array

Use push to insert element to the end of the array. You can insert multiple items by providing them in the parameter.

Treat array as a stack, first in last out data structure.

shift/unshift

Use shift to dequeue the first element from the head of the array

Use unshift to add element to the head of the array.

These two operations are slow takes O(n) because it requires shifting the array after removing or adding the element.

Queue

To use an array as a queue, use the push and shift function to dequeue and enqueue element into the queue.

Looping over array

You can use a traditional index loop

let arr = ["Apple", "Orange", "Pear"];

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

But if you only need to loop over the element without the indices then there is a shorter syntax

let fruits = ["Apple", "Orange", "Plum"];

// iterates over array elements
for (let fruit of fruits) {
  console.log(fruit);
}

Remember although you can use for ... in loop for array, it is not optimized for looping over array, so it will be slower. In addition, you will be looping over extra properties that is in array. So it is not recommended to use for ... in for array iterations, only for object property iterations.

toString()

Array object implements the toString() method to return a comma separated value of String

let arr = [1, 2, 3];

arr.toString() // returns "1,2,3"

Array object doesn't have Symbol.toPrimitive nor a valid valueOf, so they only implement toString as a catch all object to primitive conversion.

[] will become ""

[1] will become "1"

[1, 2] will become "1,2"

Comparison of array using ==

Remember that == comparison does type conversion if the type are different, and with objects it will be converted to a primitive, and in this case because array only implemented toString() it will be converted to a String. Which results in some really interesting comparison.

With == if you are comparing two object, it will only be true if they are referencing the same object.

The only exception is null == undefined is true and nothing else.

[] == [] // false, because they are different object when you created an empty array
[0] == [0] // false, again different object
0 == [] // true, because the empty array gets converted to '', and '' is converted to 0 which is true
'0' == [] // false, since empty array converts to '', those two strings are different

So how do we actually compare array? Use a loop to compare item-by-item

Array methods