I have a variable:
var code = "de";
And I have an array:
var countryList = ["de","fr","it","es"];
Could someone help me as I need to check to see if the variable is inside the countryList array – my attempt is here:
if (code instanceof countryList) {
alert('value is Array!');
}
else {
alert('Not an array');
}
but I get the following error in console.log when it’s run:
TypeError: invalid ‘instanceof’ operand countryList
jQuery has a utility function to find whether an element exist in array or not
It returns index of the value in
arrayand-1if value is not present in array. so your code can be like this