• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Array includes Method

JavaScript Array includes Method

August 30, 2022 Leave a Comment

We can use the JavaScript Array includes() method to see if an item is contained in a given array. This is pretty straightforward. Here is the code below.

someArray.includes(item));

The includes() method will return either true or false based on whether the item is found in the array.

Let’s see a simple example of this below.


Here we will have a simple array of numbers, and just use the includes() method to see if a number exists in the array.

var numsArray = [1,2,3,4,5];

console.log(numsArray.includes(2));
console.log(numsArray.includes(6));

#Output
true
false

Interactive Example of the JavaScript Array includes Method

In this example, we will have a long array of strings. We will also have an input field to let the user enter in any string that they want to check if it is included in the array.

Here is our HTML setup.


<div>["This", "blog", "is", "a", "compilation", "of", "a", "programmer’s", "findings", "in", "the", "world", "of", "software", "development", "website", "creation", "and", "automation", "of", "processes", "Working", "with", "Python", "Javascript", "PHP", "HTML", "SAS", "and", "other", "programming", "languages", "can", "allow", "us", "to", "create", "amazing", "programs", "which", "make", "our", "work", "more", "efficient", "repeatable", "and", "accurate"]</div>
<p>Type a string to see if it is contained in the array above:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="checkIfIncluded()">Get results</div>
<div id="results"></div>

First, we will add an onclick event to the submit button to run a function we will create.

We will then use the value property along with the getElementById method to get the value of the input.

We will then determine if the string is included in the array using the function we will create checkIfIncluded() below. We will add to our function below so that we can display the results to the user.

Finally, we will display the results using the textContent property.

function checkIfIncluded(){
  //The array of strings
  var arrayOfStrings = ["This", "blog", "is", "a", "compilation", "of", "a", "programmer’s", "findings", "in", "the", "world", "of", "software", "development", "website", "creation", "and", "automation", "of", "processes", "Working", "with", "Python", "Javascript", "PHP", "HTML", "SAS", "and", "other", "programming", "languages", "can", "allow", "us", "to", "create", "amazing", "programs", "which", "make", "our", "work", "more", "efficient", "repeatable", "and", "accurate"];  

  //Get the user input
  var userString = document.getElementById("userVal").value;

  //Check if the string is included in the array and display the results
  if( arrayOfStrings.includes(userString) ){
    document.getElementById("results").textContent = userString + " IS contained in the Array above.";
  } else {
    document.getElementById("results").textContent = userString + " is NOT contained in the Array above.";
  }
};  

The final code and output for this example are below:

Code Output:

[“This”, “blog”, “is”, “a”, “compilation”, “of”, “a”, “programmer’s”, “findings”, “in”, “the”, “world”, “of”, “software”, “development”, “website”, “creation”, “and”, “automation”, “of”, “processes”, “Working”, “with”, “Python”, “Javascript”, “PHP”, “HTML”, “SAS”, “and”, “other”, “programming”, “languages”, “can”, “allow”, “us”, “to”, “create”, “amazing”, “programs”, “which”, “make”, “our”, “work”, “more”, “efficient”, “repeatable”, “and”, “accurate”]

Type a string to see if it is contained in the array above:

Get results

Full Code:

<div>["This", "blog", "is", "a", "compilation", "of", "a", "programmer's", "findings", "in", "the", "world", "of", "software", "development", "website", "creation", "and", "automation", "of", "processes", "Working", "with", "Python", "Javascript", "PHP", "HTML", "SAS", "and", "other", "programming", "languages", "can", "allow", "us", "to", "create", "amazing", "programs", "which", "make", "our", "work", "more", "efficient", "repeatable", "and", "accurate"]</div>
<p>Type a string to see if it is contained in the array above:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="checkIfIncluded()">Get results</div>
<div id="results"></div>

<script>

function checkIfIncluded(){
  var arrayOfStrings = ["This", "blog", "is", "a", "compilation", "of", "a", "programmer's", "findings", "in", "the", "world", "of", "software", "development", "website", "creation", "and", "automation", "of", "processes", "Working", "with", "Python", "Javascript", "PHP", "HTML", "SAS", "and", "other", "programming", "languages", "can", "allow", "us", "to", "create", "amazing", "programs", "which", "make", "our", "work", "more", "efficient", "repeatable", "and", "accurate"];  
  var userString = document.getElementById("userVal").value;
  if( arrayOfStrings.includes(userString) ){
    document.getElementById("results").textContent = userString + " IS contained in the Array above.";
  } else {
    document.getElementById("results").textContent = userString + " is NOT contained in the Array above.";
  }
};

</script>

Hopefully this article has been useful for you to learn how to use JavaScript array includes() method.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get the Width of an Element
  • 2.  Using JavaScript to Convert Month Number to Name
  • 3.  Using JavaScript to Check If String is a Number
  • 4.  Using JavaScript to Get Date Format in yyyy mm dd
  • 5.  Subtract All Numbers in an Array Using JavaScript
  • 6.  Push Multiple Items to Array in JavaScript
  • 7.  Set Text with the textContent Property in JavaScript
  • 8.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 9.  Using JavaScript to Resize an Image
  • 10.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x