Q:

Inconsistent/ Ignored for loop in Javascript?

I have a for loop that iterates through some xml. It goes through without a hitch around 110 times, then (inexplicably) it seems like an if statement within the for loop is ignored. Any help would be greatly appreciated.
<script>
  for (let z=0; z<agearray.length;z++){
      for (let p=0; p<samplearray.length;p++){
          console.log((samplearray[p]));

          if(typeof(samplearray[p]==="object")){

              if (samplearray[p].getAttribute("age")===agearray[z]){
                  var qwerty=samplearray[p];
                  console.log("test");
                  ///blah blah blah, if statement continues
              }
          } 
       }
   }
</script>

So, the loop successfully iterates many times, with the test statement being logged as expected. Once it gets to the 111th iteration, I get the error message "Uncaught TypeError: Cannot read property 'getAttribute' of undefined" at the second if statement. If I'm correct, I would have thought that the second if statement wouldn't be able to execute unless samplearray[p] was defined. The loop can skip over other undefined objects properly, so I'm stumped. 
Any thoughts?

A:

As per the code snippet you have attached is not checking the if statement properly 
you should use 
if(typeof samplearray[p]==="object"){

instead of 
 if(typeof(samplearray[p]==="object")){

Your condition is basically checking the conditional statement samplearray[p]==="object" which will give boolean in result.

