title: Uncaught TypeError: Cannot call method 'hasAttribute' of undefined
body: <p>I am trying to check if buttons has name attribute or not.The Following line is giving result correctly.  </p>
  
  <pre><code>alert(course_view_buttons[30].hasAttribute("name"));
  </code></pre>
  
  <p>But the following code is giving an error like this-</p>
  
  <pre><code>"Uncaught TypeError: Cannot call method 'hasAttribute' of undefined "
  </code></pre>
  
  <p>The full code is here-</p>
  
  <pre><code>var course_view_buttons = document.getElementsByTagName("button");
  alert(course_view_buttons.length);
  
  var a=0;
  for (x=0;x&lt;=course_view_buttons.length;x++){
  
      if(course_view_buttons[x].hasAttribute("name");){
      a++;
  }
  }
  alert(a);
  </code></pre>
  
  <p>Please help me if anyone can.I am stuck in this for 4-5 hours. Thanks in advance.</p>