Understanding JavaScript Array methods: Unshift and Shift


Shift and Unshift are the counterparts of Push and Pop. This post provides a deep dive into their applications.

Unshift

Unshift, a unary function, ‘shifts’ the array to the right and inserts its parameters into the first slots. It then returns the new length of the array. It is the complement of push which appends values to the end of the array. Unshift instead inserts them at the head of the array

Time Complexity: O(n) since every element needs to be moved.

Invoking unshift with no values has no effect on the original array.

let array = [1, 2];
array.unshift(4);
// output: 3 (new length of the array)

array
(3) [4, 1, 2]

Unshift can be used to add multiple values; it has variadic arity.

array.unshift('a', 'b', 'c');
// output: 6 (new length of the array)

array;
(6) ['a','b','c', 4, 1, 2]

Weird Tip: Unshift and push can be used to determine the length of an array by invoking them with no parameters.

Applying Unshift to non-Array objects

Unshift (like push) is also generic and can be applied to Array-like objects.

Likewise, it relies on a length property to determine where to add new entries. If length cannot be cast to a number, unshift will create a length property starting from 0.

New length property creation

var obj = {};
[].unshift.call(obj, 1);
console.log(obj);

// {0: 1, length: 1}

Casting length to a number

var obj = { length: '1' }; // '1' is a string
[].unshift.call(obj, 1);
console.log(obj);

// {0: 1, length: 2}

Overwriting the length property

var obj = { length: 'a' };  // 'a' is non-coercible
[].unshift.call(obj, 1);
console.log(obj);

// {0: 1, length: 1}

Note: Unshift order weirdness

The ordering of elements in an array that is ‘unshifted’ depends on the unshift call mode. If all elements are passes as a block, then their original order is preserved; otherwise, the ordering is reversed.

let entries = [4, 5, 6]
let array1 = [1, 2, 3];
array1.unshift(...entries);
console.log(array1);
[4,5,6,1,2,3]

let array2 = [1, 2, 3];
for(let entry of entries) {
    array2.unshift(entry);
}
console.log(array2);
[6,5,4,1,2,3]

Note: This can be used to reverse an array elegantly.

Shift

Shift is the inverse of Pop; it removes values from the beginning of an array. Like pop, it removes the value that was shifted instead of the length of the array and it is also a nullary function with an arity of zero.

Invoking shift on an empty array returns undefined.

let array = [1, 2, 4];
array.shift();
// output: 1 (the leftmost element in the array)

array
(2) [2, 4]

Applying Shift to non-Array objects

Shift behaves just like its counterparts: unshift, push and pop.

Processing…
Awesome!

Related

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.