Arrays

Your boss might twitch a bit when you mention arrays ("a raise", get it? I strongly suggest using Mozilla developer docs for JavaScript reference materials. Sometimes technical writers have a penchant for obfuscation though, so I've just noted down some Array methods that have some finer points which are often overlooked. Not really an article that is a deal breaker for a technical interview, more like a "note to self" type of thing. Anyhoo, voila.

Array.push

When you push something to an array, the return value is the new length of the array. I used push all the time but never used considered the return value. Note to self.

Array.concat

Concat is really handy. I try to keep in mind that multiple arrays can be concatenated:

var nums = [1,2,3].concat([4,5,6],[7,8,9],[10,11,12]);
//nums will be [1,2,3,4,5,6,7,8,9,10,11,12]

Array.shift and Array.unshift

Two great functions. With these two you can implement a queue data structure.

const queue = [];
queue.unshift(1);
queue.unshift(2);
queue.unshift(3);
// [3, 2, 1]
queue.shift(); //3
queue.shift(); //2
queue.shift(); //1
// []

Array.slice

Not to be confused with splice. Slice returns a slice of the target array.

[1,2,3,4].slice(1,2);
//[2, 3]

Array.some

Handy to check if some of your array elements are truthy. You can also !Array.some to find out if none are "truthy".

Array.splice

I used to use splice to just delete elements from an array (that function and its name didn't make much sense to me before). However it is much more useful and more to it than just deleting elements.

The splice function modifies the array it is being used on. If any elements have been deleted they are returned, if nothing has been removed it returns an empty array.

Say we want a list like 1,2,3,4,5,6,7,8 but we have a list like this:

var test = [1,2,4,4,5,8]

First, lets add the number 3 to the third element of the array. As we know, arrays are zero indexed, so we want to put 3 into the third element, which would have the index of 2.

The second parameter is how many elements we want to remove, or rather, the number of elements to skip over before we "splice" the remaining section. In this case we do not want to remove any so we specify 0.

Then we pass in the item/s we want to be inserted, in this case 3. Now we have [1, 2, 3, 4, 4, 5, 8] which is looking much better.

test.splice(2,0,3);

We have 4 appearing twice, so we need to remove one. Let's remove the first. To do this we pass in the index to delete from, so starting at 0 it will be 3. Then we pass how many we want to delete, which is 1. The rest of the array is "spliced" to the first part, so in essence it deletes 1 element.

test.splice(3,1);

This would return [4] and test would be [1, 2, 3, 4, 5, 8].

Next we need to fill the gap between 5 and 8, so we need to insert 6 and 7. We can do that in one go by specifying the index to start at, how many to delete/skip over, and then the new values to insert:

test.splice(5,0,6,7);

Again, an empty array is returned as nothing has been deleted, and test is now [1, 2, 3, 4, 5, 6, 7, 8].

Array.sort

There is more to Array.sort than meets the eye.

References

More information at MDN