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.

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 the particular value.
  • etc.

If you are familiar with PHP, where you can use the in_array() function to search value in the Array and its 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


Contents

  1. Loop
  2. Array.indexOf()
  3. jQuery.inArray()
  4. Conclusion

1. Loop

First start with loop.

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

Completed Code

<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

For making your searching process simpler you can use jQuery and JavaScript inbuilt function.


2. Array.indexOf()

It is a JavaScript method which 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()

It is a jQuery function which returns the index position of the value when it finds the value otherwise -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 which you can use to search your value in 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.

Related Post

Spread the love
  • 9
  •  
  •  
  •  
  •  
  •  

4 Comments

  1. erick said:

    I think so, that in example of 2 about Array.indexOf() has error.

    var names_arr = [‘sonarika’,’yogesh’,’sumit’,’vijay’,’anil’];
    var text = “Sonarika Bhadoria”;
    // Search in Array
    alert( ‘Index : ‘ + names_arr.indexOf(‘sumit’) );
    alert( ‘Index : ‘ + names_arr.indexOf(‘vishal’) );
    // Search in String
    alert( ‘Index : ‘ + names_arr.indexOf(‘a’) ); —> should be variable text instead of names_arr ?

    October 20, 2017
    Reply
    • Yogesh Singh said:

      Hi erick,
      Thanks for mentioning it I have updated the code.

      October 20, 2017
      Reply
  2. sachin said:

    i want to update specific record update depend index based in javascript can you help please….when i assign index base then automatically all data affected all index ….

    March 2, 2018
    Reply
    • Yogesh Singh said:

      Hi Sachin,
      You can loop on the Array and use if statement to check for an Index. If an index is found then update the value.

      var arr = Array(21,43,5,65,3);
      for(var i=0;i

      March 2, 2018
      Reply

Leave a Reply

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