Check if value exists in Array – jQuery and JavaScript

In this tutorial, I show how you can check whether an Array already contains a specific value or not using Javascript and jQuery.

This requires within the program in some cases like –

  • Stop new value from insert if it already exists in an Array.
  • Execute script when the Array contains a particular value.
  • etc.

If you are familiar with PHP, where you can use the in_array() function to search value in the Array, and it returns the Boolean value ( TRUE or FALSE ).

There are inbuilt methods in jQuery and JavaScript that return the index position of the value which you can use for the search.

Check if value exists in Array – jQuery and JavaScript


Table of Content

  1. Using Loop to check value exists in Array
  2. Array.indexOf()
  3. jQuery.inArray()
  4. Conclusion

1. Using Loop to check value exists in Array

First start with a loop.

You can easily find the value within an Array by traversing the Array and check for the value.

Example

<script type='text/javascript'>

var names_arr = ['sonarika','yogesh','sumit','sonarika','vijay','anil'];
 
function checkValue(value,arr){
  var status = 'Not exist';
 
  for(var i=0; i<arr.length; i++){
    var name = arr[i];
    if(name == value){
      status = 'Exist';
      break;
    }
  }

  return status;
}
console.log('status : ' + checkValue('sonarika', names_arr) );
console.log('status : ' + checkValue('mayank', names_arr) );
</script>

Output

status : Exist
status : Not exist

To make your search process simpler you can use jQuery and JavaScript inbuilt function.


2. Array.indexOf()

It is a JavaScript method that returns the index position of the value. If it doesn’t find the value then it returns -1.

It works both with string and an Array.

Syntax –

value-from-which-search.indexOf( search-value );

Completed Code

<script type='text/javascript'>
var names_arr = ['sonarika','yogesh','sumit','vijay','anil'];
var text = "Sonarika Bhadoria";

// Search in Array
console.log( 'Index : ' + names_arr.indexOf('sumit') );
console.log( 'Index : ' + names_arr.indexOf('vishal') );

// Search in String
console.log( 'Index : ' + text.indexOf('a') );

</script>

Output

Index : 2
Index : -1
Index : 3

3. jQuery.inArray()

jQuery inArray function returns the index position of the value when it finds the value otherwise returns -1.

It also works with string and an Array.

Syntax –

jQuery.inArray( search-value, value-from-which-search);

Example

<script type='text/javascript'>
var names_arr = ['sonarika','yogesh','sumit','vijay','anil'];
var text = "Yogesh singh";
// Searching in Array
console.log( 'Index : ' + jQuery.inArray('sumit', names_arr) );
console.log( 'Index : ' + jQuery.inArray('vishal', names_arr ));
 
// Searching in String variable
console.log( 'Index : ' + jQuery.inArray( "e", text ));

</script>

Output

Index : 2
Index : -1
Index : 3

4. Conclusion

I discussed some of the methods that you can use to search your value in the existing Array. Using this you can remove or replace the duplicate values.

If you are using jQuery within your project then you can simply go with inArray() method otherwise you can indexOf() method.

If you found this tutorial helpful then don't forget to share.