jQuery Data Type How to - Check if Variable contains in the array








Question

We would like to know how to check if Variable contains in the array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
<!--from  w w w .j  av a  2  s .c o m-->
var X = 5;
var newArray = [1,2,5]
if ($.inArray(X, newArray) > -1) {
    document.writeln('is in array');
}

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: