Start a free Courses trial
to watch this video
Have you ever wondered if an array includes a specific value? There is an easy way to check by using a JavaScript array method known as includes(). Follow along as Dustin explains and demonstrates this easy-to-use method.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[MUSIC] 0:00 Hey what's up, my name is Dustin, and I'm a developer here at Treehouse. 0:09 Today, I wanna take you through using the JavaScript array method known as includes. 0:12 As the name suggests, this method checks if an array includes a specific value and 0:17 returns true or false respectively. 0:22 Let's hop into some code and see how this works. 0:24 Make sure to check out the description or teacher's notes down below for 0:26 related resources. 0:30 If you're ready, let's get into it. 0:31 First, let's check out the official MDN documentation for this method. 0:33 It's pretty straightforward. 0:37 According to MDN, the includes method determines whether an array includes 0:38 a certain value among its entries, returning true or false as appropriate. 0:43 Sounds pretty simple. 0:48 Let's practice it a bit. 0:49 I'll open up my text editor with an empty app.js file, and 0:51 I'll run this file with Node. 0:54 If you're unfamiliar with Node or don't have it installed, 0:56 you can run this code in your browser's developer console. 0:59 Let's set up a brand new array, I'll call this names and we'll throw in some names. 1:02 Let's write jamie, kandra, and taylor. 1:07 Underneath our array, let's write a console.log method, and 1:17 inside of that we can write our names array and chain on our includes method. 1:21 Inside of our includes method, let's write the string bob. 1:26 If I hit save and open up our terminal and write node app.js, 1:30 you'll see we get back false. 1:34 And this is because bob does not appear in our array. 1:36 Let's change bob with kandra. 1:39 I'll hit save and run node app.js in my terminal again, and 1:42 this time we get back true. 1:46 This is because kandra appears in our array. 1:48 This method is really straightforward but can also do some complex things. 1:51 I'll link below a video where I use the includes method to create 1:55 a dynamic search feature for a web app. 1:59 And that about covers it for the includes JavaScript array method. 2:04 I hope this guide helped and I'll see you in the next one. 2:07 Until then, have fun and happy coding. 2:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up