Check Array Contains a Value in JavaScript, with Examples

Determining Whether an Array Contains a Value

This tutorial will show you how to check whether an array contains a specific value in Javascript, with full examples.

JavaScript is a flexible programming language that runs just about everywhere. You can run JavaScript apps in web browsers, build command-line apps you can run in the terminal, mobile apps can be built using platforms like Cordova, and server processes can be written on platforms like Node.js.

Naturally, Linux is the environment of choice for many JavaScript developers and is perfect for learning tools like Node.js as well as hosting them for end users once completed.

JavaScript Arrays

An array is a type of JavaScript variable that can hold other variables, or references to other variables, in a list at a certain position.

Arrays form the backbone of most applications – they might hold to-do list items or a reference to each player on a team in a multiplayer game.

Checking if an Item Exists in an Array using the includes() Method

The following is an array containing a list of colors:

var colours = ["red", "green", "blue", "purple", "orange", "pink"];

Note that:

  • We’re declaring a variable with the var JavaScript statement
  • The array is contained in [ ] (square brackets) and each value is separated by , (standard comma)
  • Each value is a string (as it is surrounded by “”) but any kind of variable or value can be stored

To find out whether “blue” appears in the array, we can use the includes() method:

var colours = ["red", "green", "blue", "purple", "orange", "pink"]; 
var result = colours.includes("blue"); 
console.log(result); // Returns TRUE

Note that:

  • includes() returns boolean
  • We’ve used the console.log() method to output the value of the result to the console so that it can be viewed
  • We’ve added a comment: text after the // will not be interpreted by JavaScript so that you can leave yourself notes in the code

Checking if an Item Exists in an Array After a Certain Position

It’s also possible to check if a specific value appears after a specific index (position) in the array:

var colours = ["red", "green", "blue", "purple", "orange", "pink"]; 
var result = fruits.includes("blue", 4); 
console.log(result); // Returns FALSE
  • Remember that indexes are counted starting at 0, not 1: so “red” is at index 0, “blue” is at index 3
  • The result is therefore FALSE as “blue” does not appear in the array after index 4

Checking if an Item Exists in an Array Using the indexOf() Method

There is another way of checking whether an item appears in a JavaScript array. The indexOf() method will return the index of the first found value in an array, or it will return -1 if it is not present.

var colours = ["red", "green", "blue", "purple", "orange", "pink"]; 
var result = colours.indexOf("blue"); 
console.log(result); // Returns 2

Note that:

  • indexOf() returns the index (position) of the first found element which has a matching value
  • We refer to the element and value – the element is the item in the array. It has an index (position) and a value. You can have multiple elements with the same value at different positions.
  • If the resulting value of indexOf() is -1the item was not found in the array
  • If the value appears more than once in the array only the index of the first found item will be returned by indexOf()

Searching Only Certain Positions in an Array

If you want to find out whether a specific value appears at a specific index in an array, you can test the value at that index for equality using ==:

var colours = ["red", "green", "blue", "purple", "orange", "pink"]; 
var result = colours[1] == "green"; 
console.log(result); // Returns TRUE
  • You could also use indexOf() to check whether a value exists at a certain position, as it returns the index of the value if it is present in the array

Conclusion

Finding out whether an array contains a specific value is one of the basic JavaScript statements you’ll use day-to-day while developing applications. You can also find out how to remove an element from an array in Javascript in our other tutorial.

To learn about other JavaScript functions, check out other JavaScript articles!

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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