indexOf and lastIndexOf Array Methods | JavaScript Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the array index of method returns the first index at which a given element can be found inside array so for example we have here an array of names let's see at what index is even we can do cancel the klog names that index of Evon it has to be the same and you can see here we get index 1 well why would you use something like this let's say for example you want to swap out something from the array but you first need to know but what index is it so let's store this in an index so that log we get the index and now we can say names index of index will be J and now if we console that lock names you can see that even is out NJ is it keep in mind that if you're searching for something inside the array which is not there for example J but we don't have Jade yet then the index will be -1 so you can do something fun like let's say if index is greater than minus 1 so meaning that it is inside the array starting with index 0 you can console that flag yay we have J or otherwise if it's not inside the array you can consider clock whatever sad and now you can see that we are sad because J is not inside the array we can add it here at the end and now look at that yay we have J but what happens if we have for example multiple events let's remove the code let's add another event here at the end and now let's get the index names index of even and let's cancel that log the index and say which is returned it's returned one but why we have multiple events as I said in the beginning the index of method returns the first index of the element it finds inside the array but we also have the last index of array method which guess what it will return the last index from the end of the array so if we're changing this to last index off you can see now that the index is 3 0 1 2 3 so just keep in mind that index off starts from the beginning of the array and last index off starts from the end of the array all right I hope you enjoyed this tutorial and I see you in the next one bye [Music]
Info
Channel: Florin Pop
Views: 7,485
Rating: 4.8809524 out of 5
Keywords: javascript, javascript tutorial, javascript tutorial for beginners, array method, javascript arrays, learn javascript, javascript array methods, indexOf, lastIndexOf, indexOf array method, lastIndexOf array method, javascript indexOf, javascript lastIndexOf, find element inside of an array
Id: VoY6AkDZjZ4
Channel Id: undefined
Length: 3min 12sec (192 seconds)
Published: Fri Apr 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.