Javascript Data Type How to - Check if an array contains all zero(0)








Question

We would like to know how to check if an array contains all zero(0).

Answer


<!-- w  w  w.  j a  va 2  s.c om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>

var array = [0, 0, 0, 0],
    allZeros;
function zeroTest(element) {
    return element === 0;
}
allZeros = array.every(zeroTest);
document.writeln(allZeros);
document.writeln('<br/>');
array = [0, 0, 0, 1];
allZeros = array.every(zeroTest);
document.writeln(allZeros);

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

The code above is rendered as follows: