Site icon Treehouse Blog

JavaScript Array Methods: includes()

Ever wondered if an array included a specific value? There is an easy way to check this by using a JavaScript array method known as includes(). Follow along as I go over this method and show you how to use it!

First things first, what is includes()? Well, it’s a JavaScript iteration method. According to MDN:

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
MDN – includes() documentation

Consider the following array:

const names = ['jamie', 'kandra', 'taylor'];

If we wanted to check to see if the string ‘bob’ was in this array, we would just write this:

names.includes('bob');

//expected output: false

Now, let’s check if the string “kandra” is included in our names array:

names.includes('kandra');

//expected output: true

Pretty simple right? This is a very easy-to-use array method that can do some pretty powerful things. Checkout this JavaScript Search feature I was able to implement into a webpage using the includes() method!

https://teamtreehouse.com/library/javascript-search

Here are some other helpful resources:

JavaScript Basics Course:
https://teamtreehouse.com/library/javascript-basics

MDN Documentation for includes():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

That about covers this JavaScript array method. I hope you can now feel comfortable using this in your projects going forward. Have fun and happy coding!

Exit mobile version