Sitemap
Alexander Alfonso

Alexander Alfonso

Student at @zerotomasteryio | Currently learning Full Stack Web Development | Budget Tech | Motivation | Coding | Lifestyle

30 Days of JavaScript — Day 5 JavaScript Arrays and Array Methods

5 min readJan 30, 2023

--

Press enter or click to view image in full size
JavaScript Arrays and Array Methods

Hello and welcome back!

Day of 5 of 30 Days of JavaScript was focused on arrays and array methods.

In this blog post, I’m briefly going to go over what I learned in this module.

Press enter or click to view image in full size
JavaScript Arrays and Array Methods

An array is a modifiable, ordered collection of data. Arrays are used to store multiple values in a single variable. This is compared to a variable that can store only one value.

Arrays are very useful as they store multiple values in a single variable, which can condense and organize our code, making it more readable and maintainable. Arrays can contain any data type, including numbers, strings, and objects.

I also learned quite a few array methods such as pop, splice, concat, slice and quite a few more.

I won’t be going over every single method as this post would become extremely long winded.

I’ll only highlight a few of the methods that I found interesting.

Let’s start off with …

The Split Method

The split method is particularly interesting because it can take a string and turn it into an array.

The split method also has different ways of converting a string into an array.

Let’s go over some examples by examining the sample code below.

JavaScript Arrays and Array Methods

As you can see from the code above, there are 3 ways to split a string into an array.

By using split(‘’) with quotes and no space in between them, it splits the string ‘JavaScript’ by each letter.

Another way to split a string is by using split(‘,’) with quotes and a comma in between. Using this split method splits the string at the commas.

And the last method is by using split(‘ ‘) with quotes in the middle and a space in between. This method takes a sentence and turns every word in the sentence into an item in an array.

Modifying an array element

Another useful method is being able to modify the contents of an array once it’s been created. Since arrays are mutable, their contents are also mutable.

Let’s take a look at how we can do so.

JavaScript Arrays and Array Methods

We can modify an item in an array by simply declaring the variable, accessing it’s index and setting the current value to the new value.

Get Alexander Alfonso’s stories in your inbox

Join Medium for free to get updates from this writer.

In the above code, we are accessing the ‘numbers’ variable that contains an array and accessing the item at index 0 which is the number 1 and we are setting that value to the number 10.

JavaScript Arrays and Array Methods

If we console log the variable, numbers, you can see that the number 1 that was at index 0 has now been changed to the number 10.

Concatenating arrays using concat

Concatenating two arrays is simply merging two arrays together to form a single array.

To do this, we simply use the concat() method.

JavaScript Arrays and Array Methods

As you can see from the code above, there are 2 variables, firstList and secondList that each contain different arrays.

To merge the two together, we declare a third variable named thirdList and we use the concat() method.

You declare the first variable followed by .concat() and putting the second variable inside of the parenthesis.

This combines both variables into the third variable creating a single array.

The indexOf Method

Using the method indexOf() is very useful as it checks if an item exists in an array.

If an item exists in the array, it returns the index, otherwise it returns -1.

Let’s take a look at an example.

JavaScript Arrays and Array Methods

The above code is checking if them item ‘banana’ exists in the fruits array.

In this case, it does and so if you console log the variable, it returns the number 0 since ‘banana’ is at the index of 0.

Let’s wrap it up

So to summarize this post, let’s go over what we learned.

An array, what is it?

An array is a modifiable, ordered collection of data. Arrays are used to store multiple values in a single variable. This is compared to a variable that can store only one value.

Arrays are mutable which means that it’s contents can be changed and manipulated.

We also learned just a few array methods such as split, modifying, concat and indexOf.

These methods are just a few of the many array methods available.

Just by reading through this module and doing the exercises associated with the module, I can tell that this is a topic that I’ll be spending a bit more time with so I can learn more advanced array methods.

Day 5 is in the bag

And that concludes everything I’ve learned in day 5 of the 30 Days of JavaScript course.

I will be starting day 6 soon in which I will learn about loops.

I’m pretty excited for that.

Thanks for stopping by! Until next time! 👋

--

--

Alexander Alfonso
Alexander Alfonso

Written by Alexander Alfonso

Student at @zerotomasteryio | Currently learning Full Stack Web Development | Budget Tech | Motivation | Coding | Lifestyle