Javascript Data Type How to - Check item of array on "undefined"








Question

We would like to know how to check item of array on "undefined".

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from  w  ww  .ja v a  2 s .  c o  m-->
var arrayResults = [5,1,,5,6,,10];
for (var i = 0, l = arrayResults.length; i < l; i++) {
    if (typeof(arrayResults[i])=='undefined') {
         document.writeln('Element ' + i + ' is undefined.');
    };
} 

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

The code above is rendered as follows: