Skip to content

Jamie Pittman

A 30-Something Trying To Get It Right and Helping Others Along the Way

  • About
  • Blog
  • Helpers
  • Portfolio
  • Contact
  • About
  • Blog
  • Helpers
  • Portfolio
  • Contact

How to Use JavaScript Includes()–It’s Remarkably Easy

September 30, 2020September 30, 2020 Jamie Leave a comment
How to Use JavaScript Includes()–It’s Remarkably Easy

*I may be compensated if you purchase through any affiliate links on this site.*

The JavaScript array method includes(), like so many other array methods, does what its name proclaims: it will check an array to see if it includes a specific value. This array method is handy and incredibly easy to use!

via GIPHY

What does includes() do?

The JavaScript array method includes() will check an array to see if it includes the specific value that you provided. This array method returns a boolean: true if the array includes the value or false if it does not.

How do I use includes?

array.includes(valueToSearchFor, fromIndex)

The syntax is fairly simple.

The first and required parameter you pass the array method is the value that you want to search for in the array. This value is case sensitive, and it is looking for an EXACT match. Both points are important to remember!

Additionally, you can optionally pass a parameter called fromIndex which indicates from what index position you want the method to start searching. In my experience, I have rarely used fromIndex when using the array method includes().

An Example of Includes() Working It’s Magic

via GIPHY

Let’s start with an array that is very relevant to 2020.

const outOfStock = ["toilet paper", "water", "paper towels", "hand sanitizer"];

We want to check the outOfStock array to see if there is anything we need that is out of stock at the moment. We want to know if the outOfStock array include toilet paper? How do we check using the array method includes()?

outOfStock.includes("toilet paper");

We have the syntax setup, but if we recall, the array method includes() returns a boolean (true or false). How do we capture that value?

We could log the value in the console, but the best way would be to set the statement to a variable. You can then use the variable as needed. If you are just working on your own, feel free to log the value in the console as shown below to verify your answer.

const toiletPaper = outOfStock.includes("toilet paper");
console.log(toiletPaper)
//true

Key Things to Remember About Includes()

I hope that you found this to be helpful and straightforward. Here are a few key takeaways regarding includes to help you as you use it out in the wild:

  • The value you’re checking in the array is case sensitive.
  • The value you’re checking in the array must be an exact match.
  • includes() returns a boolean
  • Store the response (true/false) as a variable

Did I miss anything? What else would you include about includes()?

Photo by Clement Souchet on Unsplash

array methodsarrayscodingcoding bootcampincludesjavascript

Related Posts

4 of The Most Helpful VS Code Extensions & Features for New Web Developers
4 of The Most Helpful VS Code Extensions & Features for New Web Developers
How to Use JavaScript Reduce(): It’s Easier Than You Think
How to Use JavaScript Reduce(): It’s Easier Than You Think
forEach in JavaScript: How to Effectively Use It
forEach in JavaScript: How to Effectively Use It
Is A Coding Bootcamp Worth It? You Need To Ask Coding Bootcamp Students If You Want The Authentic Truth
Is A Coding Bootcamp Worth It? You Need To Ask Coding Bootcamp Students If You Want The Authentic Truth

Post navigation

forEach in JavaScript: How to Effectively Use It
5 Of The Most Delicious, Limited Time Trader Joe’s Fall Items This Year

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

4 of The Most Helpful VS Code Extensions & Features for New Web Developers
Blog ➤ January 1, 2021

4 of The Most Helpful VS Code Extensions & Features for New Web Developers

*I may be compensated if you purchase through any affiliate links on this site.* Things have come a long way with code editors since I started my development career four years ago. During my coding...

Read More
How to Use JavaScript Reduce(): It’s Easier Than You Think
Blog ➤ October 24, 2020

How to Use JavaScript Reduce(): It’s Easier Than You Think

*I may be compensated if you purchase through any affiliate links on this site.* I will start this post out by admitting that JavaScript reduce() was the most intimidating array method for me by far....

Read More
One of the Best North Carolina Mountain Vacations: Counting Sheep on a Beautiful Farm
Blog ➤ October 10, 2020

One of the Best North Carolina Mountain Vacations: Counting Sheep on a Beautiful Farm

*I may be compensated if you purchase through any affiliate links on this site.* Last weekend I spent four wonderful days on a North Carolina mountain vacation. My two closest friends and I headed west...

Read More
5 Of The Most Delicious, Limited Time Trader Joe’s Fall Items This Year
Blog ➤ October 3, 2020

5 Of The Most Delicious, Limited Time Trader Joe’s Fall Items This Year

*I may be compensated if you purchase through any affiliate links on this site.* It’s fall y’all! And for me, one of my favorite parts is about this season is Trader Joe’s fall items. It’s...

Read More
How to Use JavaScript Includes()–It’s Remarkably Easy
Blog ➤ September 30, 2020

How to Use JavaScript Includes()–It’s Remarkably Easy

*I may be compensated if you purchase through any affiliate links on this site.* The JavaScript array method includes(), like so many other array methods, does what its name proclaims: it will check an array...

Read More

Subscribe

Stay up to date with our latest posts and news!

Categories

  • A Better Me (1)
  • Blog (19)
  • Career (9)
  • Coding (14)
  • Finance (1)
  • Food (1)
  • Lifestyle (1)
  • Pages (4)
  • Travel (1)
  • Travel (1)
  • Tutorials (2)
  • Wellness (1)

Instagram

Hey, There!

Hey, There!

Jamie Pittman, Founder

*I may be compensated if you purchase through any affiliate links on this site.*

Welcome to my blog! Here you’ll find stories from my life, advice about your career, and blogs about all the things I love. Contact me!

Recent Posts

  • 4 of The Most Helpful VS Code Extensions & Features for New Web Developers
  • How to Use JavaScript Reduce(): It’s Easier Than You Think
  • One of the Best North Carolina Mountain Vacations: Counting Sheep on a Beautiful Farm
  • 5 Of The Most Delicious, Limited Time Trader Joe’s Fall Items This Year
  • How to Use JavaScript Includes()–It’s Remarkably Easy
  • About
  • Blog
  • Helpers
  • Portfolio
  • Contact
© 2021 | Designed by Little Theme Shop