Check if a value exists in a JavaScript array.

This is a tutorial on how to check if a given value exists inside a JavaScript array. This can be useful if you want to avoid adding duplicate values to an array. It can also be used for client side searches and whatnot.

Let’s jump right in and take a look at the following function:

/**
 * JavaScript function that checks to see whether an array
 * contains a certain value.
 * @param arr The array you want to search.
 * @param searchFor The value you want to search for.
 * @returns {boolean} TRUE if the value exists in the array. FALSE otherwise.
 */
function arrayContains(arr, searchFor){
    if(typeof arr.includes == 'undefined'){
        var arrLength = arr.length;
        for (var i = 0; i < arrLength; i++) {
            if (arr[i] === searchFor) {
                return true;
            }
        }
        return false;
    }
    return arr.includes(searchFor);
}

Above, we created a custom JavaScript function called arrayContains. This function takes in two parameters: The array that you want to search and the value that you want to search for. If the value you’re searching for is found inside the array, this function will return a boolean TRUE value. Otherwise, it will return FALSE.

At the start of our function, we check to see if the inbuilt Array.prototype.includes() method exists. If it does not exist, then we loop through the array manually and check if the element exists.

I added this typeof check in order to provide some sort of backwards compatibility for older browsers. Truth be told, this code block will rarely execute, as JavaScript’s includes() method is supported by almost all modern web browsers.

Note that both includes() method and the manual search in this custom function are case sensitive.

Take a look at the following examples:

//An example JavaScript array.
var myArray = new Array(
    'John', 'Sarah', 'Lee', 'Aarav', 'Miguel'
);

console.log(arrayContains(myArray, 'Lee')); //returns true
console.log(arrayContains(myArray, 'lee')); //returns false
console.log(arrayContains(myArray, 'Patrick')); //returns false

In the code snippet above, “lee” returns false because of the lowercase L. “Patrick” returns false because that string does not exist inside the array.

Using jQuery to check if a value exists in a JavaScript array.

If you have committed yourself to using the jQuery library, then you can simply use the inArray() method like so:

//Using the JQuery inArray method
if(jQuery.inArray("Sarah", myArray) !== -1){
    console.log('Sarah found in array.');
}

The $.inArray() method will return a -1 value if the element was not found inside the array. Unlike the function in our first example, it does not return any boolean values.

If the value does exist inside the array, then $.inArray() will not return -1. Hence the reason we check to see if the return value is -1 or not.

Note that this method also carries out a strict comparison. i.e. It is case sensitive.

Hopefully, you found this guide useful!