I currently have an if statement like this:
if (x==a||x==b||x==c||x==d||x==e) {
   alert('Hello World!')
};
How can I instead test if x equals any value in an array such as [a,b,c,d,e]?
Thank you!
I currently have an if statement like this:
if (x==a||x==b||x==c||x==d||x==e) {
   alert('Hello World!')
};
How can I instead test if x equals any value in an array such as [a,b,c,d,e]?
Thank you!
 
    
     
    
    You can use
if([a,b,c,d,e].includes(x)) {
    // ...
}
or
if([a,b,c,d,e].indexOf(x) !== -1) {
    // ...
}
 
    
    You can use the following code:
<script>
    var arr = [ 4, "Pete", 8, "John" ];
    var $spans = $("span");
    $spans.eq(0).text(jQuery.inArray("John", arr));
    $spans.eq(1).text(jQuery.inArray(4, arr));
    $spans.eq(2).text(jQuery.inArray("Karl", arr));
    $spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
</script>
Read this link for more information about it
Hope this helps you.
check out jquery function inArray(): http://api.jquery.com/jQuery.inArray/
 
    
    Try this helper function:
function in_array(needle,haystack) {
    if( haystack.indexOf) return haystack.indexOf(needle) > -1;
    for( var i=0, l=haystack.length, i<l; i++) if(haystack[i] == needle) return true;
    return false;
}
This makes use of the built-in indexOf if available, otherwise it iterates manuatlly.
