Switch to full style
JavaScript code examples
Post a reply

check array contains a value using javascript

Sun Sep 18, 2011 1:12 am

check array contains a value using JavaScript
Code:

<html>
<
title>check array contains</title>
<
head>
<
script language="JavaScript">
 

function checkContains
(mycolor)
{
 var colorArr= Array("RED", "BLUE", "YELLOW");

 
 
if(contains(colorArr,mycolor)) {

   document.write(mycolor+'<b> Color</b> is there.');
}else{
 
   document
.write('Not there.');
}

}
 

// contains function
function contains(arr, findValue) {
    var i = arr.length;
     
    while 
(i--) {
        if (arr[i] === findValue) return true;
    }
    return false;
}
 
</script>
</head>

<body>

 
<SCRIPT language="JavaScript">
 
checkContains("RED");
 
</SCRIPT>

</body>
</HTML>




Post a reply
  Related Posts  to : check array contains a value using javascript
 Array difference for associate array     -  
 Check DNS error     -  
 check if domain name is taken by php     -  
 check if number is odd or even     -  
 compare an array with another array?     -  
 check empty number     -  
 check request was GET or POST     -  
 code for check availability     -  
 check keyboard events using C++     -  
 check folder content using asp     -  

Topic Tags

JavaScript Arrays