title: Getting error " 'getAttribute' of undefined " for the last element in the loop
body: <p>Can not understand why I'm get an error for the last element in the loop? I see other questions on SOF, but I does not found an answer for my case.</p>
  
  <p>I'm trying to get the list of <strong>tag a</strong> elemnents, but </p>
  
  <pre><code>&lt;script&gt;
  
      let a = document.body.getElementsByTagName('a');
  
      function getList(list) {
  
          let ii;
          for (let i = 0; i , a.length; i++) {
  
              if (a[i].getAttribute('href')) {
                  ii = a[i].getAttribute('href');
                  console.log(ii); // get the list of elements that contains attr "href". 
                                   // and an Error for the last element -
                                   // error 'getAttribute' of undefined.
              }
          }
  
          return ii;
      }
  
      getList(); // start for test
  
  &lt;/script&gt;
  
  
  &lt;body&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href="http://google.com"&gt;http://google.com&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="/tutorial"&gt;/tutorial.html&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="local/path"&gt;local/path&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/body&gt;
  </code></pre>