March 11th, 2020
Before we begin, I’d like to make sure you’ve checked out yesterday’s story. If you have, well done! Today we’ll be talking about arrays! If you haven’t, feel free to check out yesterday's topic on the link below!
Javascript in 15 Days : 2nd Review Day
I believe everyone knows what bookshelves are right? If you don't, basically that drawer or shelf to store books, like those in libraries and book shops. Well, arrays can also be referred as bookshelves. In a way that although all it stores are books, each books may not necessarily be identical. For example, their titles, genres, authors, publishers, etc. Now, quick recap. When we learned about variables, we learned about number, string, null, undefined, and boolean. Just like bookshelves, arrays can hold different data types! For example, here's an example of declaring an array and printing it to the console (terminal).
Now, a quick recap on strings. I believe you all know that strings are a set of characters, and are immutable. which means that you can access a portion of the string but can't change the value of it, as shown below.
Now, same like strings, you can access the items (elements) inside the array based on index. The only difference is that now, with array, it's mutable. Meaning that now, you can override the elements in the array based on index, and as shown below, we will try to print the element at a specific index, a portion of the array, and replace a portion of array with something else, as shown below.
Okay, so let me walk through you the process, line by line. First of all, I initialized the array as the variable array. Next up, I printed the first element of the array (0 being the first index). Then, we used the slice method, which takes two arguments, start
and end
, both being of type number and then returns a portion starting from the number start
, until, but not inclusive of the number end
. So, in our example, the start
being 0 and the end
being 3, now it returns from index 0 ~ 2 (because not including 3, which is the end
). And as expected, does exactly that. Next up, we reassigned the value of the 2nd element, to be the string Jefferson
, and now, when we print the whole array, the array have been modified, as expected. Now lastly, we tried using the splice method, which takes at least two arguments, with no limit whatsover. So it consists of the number start
, the number deleteCount
and the rest are the elements to be injected. So, how splice works is basically starting from the start
, it deletes as much elements, starting from start
and inserts the remaining arguments into the same exact position, which can be seen in the example above, again, as expected. Which in the end gets printed out and can be seen the newly modified array.
So, as you can see, you can do quite a lot of things with arrays! Now, it's okay if it all seems new to you, but as time goes, with a lot of practice, before you know it, you'll be used to arrays! Now, next up we are going to be discussing about Multidimensional Arrays, which is also called nested arrays(arrays in arrays), and how to use them to our advantage. So, see you tomorrow and have a great day! Happy coding!