JavaScript Data Structures: Arrays (pt. 3)

Esther 승연 Kang
3 min readDec 6, 2020

Here we are — the third and final part of the three-part series just for Arrays in JavaScript! But honestly, there is always going to be more information on arrays that I may never touch, so take this series with a grain of salt and know that it is more of a beginner’s guide to arrays.

As mentioned in the last post, I will be talking about some of the commonly used methods/functions when dealing with arrays. Now, like I just wrote above, I will not be going over every single method/function there is — just the commonly-used ones that I feel like are pretty essential to know!

So, let’s get started!

two cats dj-ing

Finding Information About the Array

You can use certain operations to find out information about the array.

The most commonly used operation is the .length() operation. This one is pretty self-explanatory. It returns the number of elements within an array — the length of an array.

let array1 = [1, 2, 3, 4, 5, 'a', 'b', 'c']console.log(array1.length());
//=> 8 (because there are 8 elements in this array)

You can also find the index of an element within an array by using the .indexOf() operation. Another also self-explanatory operation — it returns you the index of the element of which you call the operation on.

console.log(array1.indexOf('b'));
//=> 6

Basic Array Manipulation

Two common operations used in algorithmic functions are the .split() and .join() operations. These are used hand-in-hand with strings.

.split() will split a string into an array — of separate strings. .join() will join an array of strings back into one string.

let splitExample = "split".split();console.log(splitExample);
//=> ['s', 'p', 'l', 'i', 't']
let joinExample = ['j', 'o', 'i', 'n'].join();console.log(joinExample);
//=> 'join'

You can also reverse the order of the elements in an array with .reverse().

console.log(['r', 'e', 'v', 'e', 'r', 's', 'e'].reverse());
//=> ['e', 's', 'r', 'e', 'v', 'e', 'r']

.sort() will sort an array alphabetically (and when all elements in an array are strings).

let array2 = ['banana', 'kiwi', 'apple', 'strawberry', 'mango'];
let sortedArray = array2.sort();
console.log(sortedArray);
//=> ['apple', 'banana', 'kiwi', 'mango', 'strawberry']

.concat() will merge two arrays and create a new array.

let array3 = ['esther', 'stephanie'];
let array4 = ['simon', 'andrew'];
let array5 = array3.concat(array4);
console.log(array5);
//=> ['esther', 'stephanie', 'simon', 'andrew']

The last thing I am going to briefly mention is the delete operation. Yes, it literally deletes elements from an array — BUT, it leaves undefined holes. So, this is not the best way to remove elements from an array. It is better to use .pop(), .shift(), or .splice(), all of which I talk about in this post here.

let array6 = ['a', 'b', 'c', 'd', 'e'];
delete array6[2];
console.log(array6);
//=> ['a', 'b', undefined, 'd', 'e']

And there we have it! Obviously, like I mentioned before, this is not all there is to know about JavaScript arrays. In a later post (which, I don’t know when exactly I’ll write), I’ll talk about loops which are commonly used with arrays.

Next week I will be talking about Hash Tables, or Maps. Stay tuned!

--

--

Esther 승연 Kang

Software Engineer | Flatiron School Alum | Dancer | Looking for Work!