Javascript Data Type How to - Filter out NaN value








Question

We would like to know how to filter out NaN value.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!-- www  .  j av  a 2 s .  c  om-->
    var nums = [10, "aaa", 44, 16];
    var numsLen = nums.length;
    var total = 0;
    while(numsLen--){
        total += (isNaN(parseInt(nums[numsLen], 10)) ? 0 : parseInt(nums[numsLen], 10));
    }
    console.log(total);
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: