Array Includes method in ES2016 - ES7

Akash Gutha
InstructorAkash Gutha
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

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.

[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.

egghead
egghead
~ 13 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today