JavaScript
Javascript Check If A Value Is In An Array Examples
It’s relatively very easy in Javascript to check if a value is in an array. All you need is the array that you want to check and the value that you want to check against. That’s it. Using just those 2 pieces of information, you can easily check if a value is in an array or not using JavaScript. Read on to find out more.
How to use Javascript to Check if a value is in an array – Examples
Example 1: Here is the full JavaScript code you need to check if a value is in an array or not:
my_array = Array();
//To find if a value or element exists in an array
if (my_array.indexOf(‘find_this_value’) != -1)
{
alert(‘Value exists in array.’);
}
//To find if a value or element DOES NOT exist in an array
if (my_array.indexOf(‘find_this_value’) == -1)
{
alert(‘Value does not exist in array.’);
}Example 2: Check whether a string is a part of an array value i.e. match a string with just part of the array value
function search_for_string_in_array(search_for_string, array_to_search)
{
for (var i=0; i<array_to_search.length; i++)
{
if (array_to_search[i].match(search_for_string))
{
return 'Value exists in array';
}
}
return 'Value does NOT exist in array';
}
var my_array = ["test one", "test two", "test three"];
alert( search_for_string_in_array ('two', my_array) );Simply pass the value to search for and the array to the function and it will tell you whether the string exists as a part of an array value or not.
Simple, isn’t it?
Do you know of any other way to check if a value or an element exists in an array or not in JavaScript? If yes, please share your tips with us by commenting below.
For the value “find_this_value” in your code, if my array contains duplicate values, and I want to find the index values of all these values how should I proceed.
July 25, 2012 at 4:02 am
First of all thank you very much robert for your help
it really helped I was finding for this solution for 2 hours but finally I got it I faced 3 or 4 problems in doing this if you can help me a bit more if I write this code like this then why this is not working fine
var srch = 'val four';
var arr = ['val one', 'val two', 'val three', 'val four', 'val five'];
for(var i = 0; i <= arr.length; i++)
{
if(srch == temp[ii])
{
alert('this value is in array');
}
else {
alert('this value is not in array');
}
please tell me that why this is not working..
thanks again
September 26, 2012 at 7:19 pm
sorry for mistake instead of this code
if(srch == temp[ii])
this is
if(srch == temp[i])
now please help
September 26, 2012 at 7:23 pm
Hi Tahir,
I did find some mistakes with your code. You have corrected the first issue and that was of the double “i”. Next, you are declaring array as var arr, but you are using an incorrect array name (temp). You don’t have a temp array with values in it. You have an “arr” array. Other than that it should work. I fixed the entire code. You can find it below:
var srch = 'val four';var arr = ['val one', 'val two', 'val three', 'val four', 'val five'];
for(var i = 0; i<= arr.length; i++) { if(srch == arr[i]) { alert('this value is in array'); } else { alert('this value is not in array'); } }
My code above it works for me. So try out the above code & let me know how it goes.
Robert
September 26, 2012 at 7:47 pm
Hi, nice explanation. But how would you match part of the string e.g. I wanted to find if my array element contains ‘two’ .
var arr= [“test one”, “test two”, “test three”];
if (arr.indexOf(‘two’) != -1)
{
alert(‘Value exists in array.’);
}
zaisharul
March 14, 2013 at 12:57 pm
Hi,
This is possible. I have updated the original article with Example 2. Simply review Example 2, follow the instructions and let me know if that solves your issue.
Rocky Dev
March 14, 2013 at 7:53 pm
Thanx alot, u rilly ar a life saver, av been on dis 4 wiks. God bless.
May 11, 2013 at 2:27 pm
i have some value for array and i give the value propmt for A. when i give User input A out will display ->apple-2,grape-6 pls help me
vins
June 26, 2014 at 10:54 am