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.


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

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:

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

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

Categorized in:

JavaScript,

Last Update: March 11, 2024