02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

indexOf vs. lastIndexOf

We know how to grab the last thing and the first thing but how do we find things in the middle. this is exactly where indexOf and lastIndexOf are very handy.

Question: What is the difference between indexOf and lastIndexOf?

It's very simple: an array is like a line. imagine that there are 20 people standing in a line every one has there own name. when we ask for indexOf we start looking for "ben" from the start of the line starting at index 0 and going on until we find "ben" while when we use lastIndexOf we start from the end of the line. what's the catch you might be thinking. the best way to answer that is through a question "what if there are more then one 'ben' in the line? as it returns the index of the first thing it hits different values would return depending on our choice to start from the end or the start looking for that match. there are a few more layers to this and that we will explore more in the video.

Video Transcript

00:00We've seen how we could remove things from the beginning of an array or end of the array and we've seen also how to add or
00:06remove things from the beginning or the end.
00:08But sometimes we might encounter a scenario where we need access to something that's in the middle of the array or in a position
00:15that's not in the edge.
00:17Before we could actually do that, we have to figure out where is that item.
00:22And I just deleted again just our implementations and I left our functions in our two arrays.
00:27And the next item I wanted to talk about is how could, for example, let's see how could we find Chris.
00:33Now we don't really know. Let's assume that we don't know where Chris is in our array even though we know he's in position number two.
00:39But let's assume that we don't.
00:41There are a few methods in in our array which enable us to find things and
00:46let's just write here an index and the index will be a UN because it could be only up well
00:52it actually could be an int because if it cannot find an item, it will return minus one.
00:57So let's see how we do that. First of all, I'm gonna assign the value that comes back from this method
01:03into our index and we're gonna ask from a users
01:08dot
01:10index of and
01:12then I could write inside of here my search and
01:15currently because I'm working with strings, I could just type directly in that string that I'm looking for. For example, let's look for Chris and
01:25let's just trace it out and
01:33we see that the value that we're getting back is two. Now one thing to note is that Flash is case sensitive and especially index of,
01:40index of is
01:42basically testing out in
01:45this level as we've seen in one of our earlier operators. Behind the scenes is actually checking if it's an exact exact match.
01:51Not almost, not nearly. It has to be exactly the same thing.
01:56In other words, if we would have here, for example, if we wanted to search in our user levels,
02:02right? Let's try to see this. If I would try and for example, instead of doing this,
02:06I'm just gonna comment it out for a second and
02:09we're going to ask for our user level. So a user
02:15level dot
02:17index of and as we said that
02:20Booleans are basically the same thing as numbers only zero and one.
02:24So what would happen if we would search for the index of zero? If we would search for this index of zero,
02:32let's see what value would come back and then let's try to understand why that value came back and the value that comes back is
02:37minus one and the reason is that it's minus one is because as we said it's an explicit match and
02:44because although zero and false are the same, they're not explicitly the same because there's a conversion that happens.
02:52Thus, if we would change that to be false,
02:55we would get a value back.
02:57Now, as you could see right here in this scenario, contrary to our example with Chris, there are few falses.
03:04What if we wanted to get all the falses or a false from a specific point and that's why we have another
03:12property for our index of. Index of and kind of like always in when you're in flash,
03:16you could always open up a bracket again to just get hints again.
03:19And you can see that our first item is our search element,
03:22but we could if we wanted to we could have added a second item which would then be from what index. Now
03:29we know already that there is one at one.
03:31So if we wanted to we could have typed one again and by typing one, let's see what happens.
03:36Well, one is still the same. So we actually probably want to go for two, right?
03:40We want to start at two because we know that there's something at one.
03:42So it automatically matched up and returned one.
03:45But if we start from index number two and now it tells us that it found something in two.
03:49And if we put now index number three, what would happen?
03:53It would return minus one because it can't find anything, right?
03:56So might this is three can't find can't find and it's over.
04:01Okay, so we could use in our searches. We could use our index or not.
04:05We could choose to or not in our scenario.
04:07We knew that our users are always unique names, although in the real world, some users might be not unique names.
04:14Okay, now let's say for example, what would happen if we would add just one more user into our array and we'll just add well
04:21Let's not add it directly. Let's use one of our functions that we've used before which was push user, right?
04:26We want to add to the end of the list. So I'm just gonna call push user and I'm gonna send it Ben
04:33Which is the same user as we had before and this Ben is not going to have the same rates
04:38okay, now I know that my user is the last user and I already know that if we would call it our a users dot
04:47index of
04:49Ben
04:51right
04:52Now we're gonna get the index right so we could if we wanted to we could have now take that value
04:58Wrap it inside of an a user level. So if we wanted to we could have done something like this
05:04What does that do? What does that mean? So first of all, I'm taking my a user level
05:08I'm opening a bracket and closing a bracket as we approach arrays and
05:12The value that comes back from a users that index of is then pushed into so first off the first value
05:19That should come back is zero, right and that zero will feed into a user level
05:24Thus, what should we get in back? We should get back the value true
05:28Let's see if that is the case
05:30And indeed that is the case. So let me just even add here before that. Let's just grab this value and
05:38We could say right here. We'll just type your Ben at
05:44position oh
05:47And actually we want to let's just save that that index into a variable before we continue. So I'm just gonna put here a little
05:55index again
05:57Which would be an end because we said it could return also a negative value
06:01I'm just gonna grab
06:03my index of Ben and
06:07Then we we're gonna set Ben at position
06:12Index is at level and let's see what would come back. Let's see if this wouldn't generate an error
06:26And here we go. So Bennett position zero is at level true
06:31Okay. Now, what would be on the other other way around?
06:34What if we wanted to find out the last item last instance of Ben? So there might be even a hundred instances
06:39But we know that our Ben that is relevant to us would be the last Ben. So let's see how we would do that
06:45It's actually really easy. We already know how to do it
06:48Only one thing is missing the last index of which is basically exactly the name of our method
06:55so a users dot last index of and same deal comes here by default is
07:03Gonna start from the end
07:04Now if we wanted it to start for anywhere else
07:07We could set it to start from somewhere else same as we could have set for our index of only difference
07:12Is that index of starts from the beginning while last index of starts from the end?
07:17So we're gonna grab our last index of and our search item is still Ben
07:22And we don't really care about our last index because we want to start from the end
07:27We don't want to start from anywhere else. Okay, so let's trace this one out
07:32So let me just grab that and trace this item out
07:37And what we should get in our second trace now is Ben at position 5 is at level false
07:43And that is true
07:46Because we didn't set any value
07:49But if we wanted to we could set him to be true and let's see if it just updates and it definitely does
07:54So we could get rid of that. We already confirmed it now
07:58Let's see what would happen if we would put a negative value such as minus 2
08:03Minus 2 what does that mean to flash in this scenario minus 2 basically says go to the end two points before the end
08:10Basically meaning that instead of starting from the end. We're gonna start one before basically starting from art
08:17Skipping our last bin and thus if we would run our application one more time now
08:23We should see that it's returning to us Ben from position 0 again because we skipped the item and minus 1
08:31Now if we set it to minus 1 is actually exactly that minus 1 is exactly the last index
08:37Right. So it's the last index is minus 1 if we're starting from the end
08:41So it's it's basically the value our current index minus that value because basically we're starting from the last index
08:49right
08:51So what would that mean? So that should be just about right right because we're getting back a same value
08:57What would happen on the other hand if we go into our index up and do the same thing?
09:01What would happen if we would start from minus 1?
09:04Let's see. What would happen?
09:06If you would start from minus 1 then it's basically saying
09:10Index of but starting from the end
09:13so you could see that the correlation between index of and last index of are very very close and
09:18In a way you could always use either or the only big difference between them is that index of goes forward
09:24Well last index of goes backwards

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!