Lesson 19 IndexOf, Slice Array Method in Javascript

In this lesson, we are going to discuss two more methods of Array, indexOf() method and slice() method, and we are going to discuss how we can find if the particular item exists in the array list or not, and we will also learn how we can copy particular items from arrays and store in some other array.

IndexOf() Method

So the next method we are going to discuss there is indexOf, so this method takes an arguments like string or a number and tries to find that arguments in a given array list and if it finds it then it will return the index where it’s found and return -1 if our arguments are not present in the array list.

Syntax for IndexOf() Method:

Let’s suppose an example here, we have an array of fruits name and I have a list of four different fruits.

var fruits = ["Strawberry", "Blueberry", "Grapes", "Apple"];
fruits.indexOf("Grapes");

img
So if I want to know the index of Grapes, which is the third item and having index 2, so it returns me two.

Example

Let’s try another example and in this example, we have two Blueberry in the list on at the index 1 and another at index 4, so let’s see what happened if we try to find the index of Blueberry.

var fruits = ["Strawberry", "Blueberry", "Grapes", "Apple", "Blueberry"];
fruits.indexOf("Blueberry");

img So if we tried to find the index of Blueberry we got the index of first Blueberry which is 1.

Example

Now let’s see what happened if I want to find something that does not exist in the array list.

var fruits = ["Strawberry", "Blueberry", "Grapes", "Apple", "Blueberry"];

So if I want to find banana which is not present in my array list, let's see how it behave if it does not find a banana.
img So I got -1 which means that Banana is not present in our array list.

Slice() Method

So the next method we are going to discuss here is a slice(), so we use slice() method to copy different portions of an array.
So here we have a array list below,

var fruits = ["Strawberry", "Blueberry", "Blackberry", "Grapes", "Apple"];

So if I want to copy all the Berries and create another array list only for Berries.
So this is how we can do that.
So we create a new array with the name of Berries to store all the berries fruit.

var berries = fruits.slice(0, 2);

So here we have used fruits.slice to take the copy of all the berries from fruits list, in slice we have two arguments, the first one is the starting index which is 0 where strawberry exist, its where the cut begins and the second argument is where cut stop which is 3, where Grapes exist but grapes are not included.
So it just copies all the items from strawberry to Blackberry and stored in the Berries Array.
img So you can notice in this illustration, we have berries array which consists of Strawberry, Blueberry, and Blackberry.
We have a fruits array which still contains all the fruits including all the berries.

Example

So here we are creating a duplicate array, so to do that we need to use slice with empty parentheses.
So this is how you can create a duplicate array.

var fruits = ["Strawberry", "Blueberry", "Blackberry", "Grapes", "Apple"];
var fruitlist = fruits.slice();

img So here we have created the duplicate of the fruits array.

Conclusion

In this lesson, we have discussed two more methods of Array, indexOf() method and slice() method, and we have learned how to find whether a particular item exists in the array list or not, and we have learned how we can copy particular items from arrays and store in some other array.

> Continue to Lesson 20

Javascript Lessons for beginners

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?