Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Array Includes method in ES2016 - ES7

    Akash GuthaAkash Gutha

    Introduction to the new includes method included in the ES2016 specification for the Array data structure. Review of the previous methods followed, also some complicated examples to get out heads around the new functionalities provided using the includes function.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:00 Let's declare an array X and populate it with strings, animals, cows, and ducks. Now, let's use the include statement to find, if the string cows exist in the array. We'll log this using the console.log statement.

    00:29 Now if you run this, the output should be true. As you can see, the output is true. Now, let's look at the old method that was used to achieve this.

    00:41 We used to use the indexer function to find the index of the input in the given array, and check if the index is greater than zero. Now if you go ahead and run this, you can see that both the methods yield the same result.

    01:01 Now, we'll go ahead and pass in a string that does not exist in the array. Let's pass in a string goats, and if you go ahead and run this, you can see that the statement yields false.

    01:22 Let's take a look at some more complicated examples. I declared an array X with single object in it that is Not a Number. If we log this statement, we'll get true, since Not a Number is an existing value in the X array.

    01:42 Contrarily, if we use the older method, the result is false. This might be a handy result to be remembered.

    01:50 The includes method does not differentiate between a negative zero and a positive zero. Hence, it will return true. The includes method has not only be added for the simple array type. It has also been added to all the typed array types.

    02:07 In this specific case, we made an eight bit unsigned integer array using the following elements. Now we are checking, if the element eight is included in the array Y. As you can see, the result is true, which means the element eight is present in the array Y.