So I think we can all agree that we use at least one data structure without realizing it almost every day, if you are a programmer, and that is the array. I won't be going to in depth about arrays since well I'm pretty comfortable with using arrays and their methods. So you can create an array by doing 1 of the following:
- Using the Array() constructor:
let array = new Array();
- Array literal syntax:
let array = ;Both of these ways are valid but I would argue that the vast majority of us, including myself use the literal syntax.
- indexOf Wow so I guess just looking at a full list I use much more than I thought without even thinking about it.
I'm not going to go super in depth and or create these but we'll go ahead and talk about a few of them. By default when an array is instantiated, the length is set to the number of elements passed in the constructor, if none obviously the length will start at 0.
forEach(), one of my favorites and probably the best way to screw up your time space complexity (as I clearly do the vast majority of the time), but I digress. The purpose of this method is to iterate over every item of the array. It differs from the
map() method which does indeed iterates over array items but the main difference is that map returns a new array (adding to space complexity).
push() adds a new element to the end of the array where
unshift() adds that same element to the beginning of the array and in both of these instances the length of the array is returned. Another set of methods that have similar actions are
shift() which removes the first element of the array and
pop() which removes the last element of the array. And in both shift and pop, the element that is removed from the array is returned.
includes() under the hood I believe (and please if I'm wrong correct me in the comments below), loops through the array (possibly using forEach) and returns whether or not a specified value lives in the array.
indexOf in a similar fashion will return not only if the value is there but the values index. So why not just always use indexOf? As I was typing this I realized I honestly don't know the answer and use includes far more and well after a quick google search I found this: Stack Overflow and turns out I need to not use includes and use indexOf more not only for readability, but for it has better browser support. It's also faster I read. So literally learning as I'm typing this out. Pretty cool and mental note has been made.
So I'm going to stop there with the methods and wrap this up. We now know that we've been using data structures and their properties and methods without even realizing it. I feel smarter already, not really but hey I will try and make myself feel better. Full List of Array Methods a full list of array methods can be found here and is a super awesome resource. I'll probably do one post over reduce since I still have no idea what it does and how it works and I see it in snippets all over the interweb. But for now I feel a little better about this topic than I did when I woke up today.