Learn JavaScript #14 | Arrays Part 3

JosephDelgadillo
58 followers
11 Video Views·Aug 30, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
In this JavaScript tutorial video we will finish up with arrays. I will show you how to manipulate an array by it's index number, and we will write an if statement to print out different strings based on whether an item is present in the array.
Learn more about my courses at https://josephdelgadillo.com
Let me clear this out before we start on the next video. Alright, so, what we're going
to do in this video is learn how to get the index number of an array, and then
how we can use that. So, I'm going to type hobbies, I'm going to store this in
a variable. So, I'm going to type indexNum, and it's going to be equal to our
array. So hobbies, actually let me refresh entirely here so we have the full
hobbies list. What we're going to do is we're going to do is get the index number of this,
so counting from 0 this will be 0, 1, 2. So, this has an index number of 2 in the
array, reading. So what we're going to do is we're going to create the variable
indexNum, it's going to be equal to
hobbies.indexOf, and we're going to be looking for 'reading.' So now if I type
indexNum it's going to return the number 2 because that is the index
number 2 inside of the array. So how we remove items from an array specifically,
so if we wanted to remove item at index number 5 you know we could do that as
well. For right now, we're going to remove index number 2. So we're going to remove
reading from hobbies, and how we're going to do that is we are going to type
hobbies.splice. And now this method takes two parameters. The first one is
the position that we start at, and so if we typed 2 here it's going to start at
reading. So this is going to be the first one that it removes, and then we can remove
however many numbers at that point. So if I wanted to remove just one, we're just
going to remove a reading from this list, the two variables that it takes in is 2
and 1. Now, let's pretend that I don't know that I'm removing index number 2. So
I'm going to use our variable indexNum, and we're going to remove reading. Now if
I type hobbies you'll see that that has been removed. Now,
I'm going to remove gaming, programming, and music using the same methods. So
hobbies.splice, I'm going to splice from position one, I'm going to remove three
items. Now if I type hobbies we get blacksmithing and archery. This is now,
these are now our hobbies. So, that's how to get started with arrays. Now
there's one other practical use for index of, and that's to evaluate whether
or not a certain item is in the array. So, let's go ahead and write an if statement,
and I'm going to write this in here, and the if statement is going to be if
hobbies.indexOf('reading') is greater than minus 1, that means reading is in
the array. So I'm going to console.log "I like reading!" I'm going to remove that
for each, and then we're going to test another. if(hobbies.indexOf
let's say 'sports') is more than minus 1 that means sports is in the array, which
it's not, so what we're going to do is else console.log("not in array")
I'm going to save this and I'm going to refresh it, and we'll see I like reading
and then not in array because this returns an indexed number that is 0 or
greater. This can either be at index 0 or greater. If this is not found at all,
the index of method is going to return the number minus 1. So, we could
alternatively do this to test if something is not in the array, oops, we
can say if it equals minus 1. I'm going to save it, I'm going to refresh, and it
still says not in array. Now I used three equal signs here. This basically
means not found, so we're checking if hobbies, sports,
is not found. Now, we can do this with two, but that would
return true if this was returned, right. So we're not comparing types, in that
instance, we're comparing values, and certain values can be matched across
types. For instance, true and true by using the two equal signs. So using three
equal signs means does the value match and is it the same type, and so it's
always safest to do this if you are looking for something specific in an
application. If you're making any type of comparison that you know it needs to be
one or the other, and it needs to be the same type, that's when you're going to
use these. Alternatively, if it's not the same type you're going to use
exclamation mark equals equals, and that's how we're going to work with that.
So, I think we're all done with the arrays, next I would really like to move on to
classes and building objects.
