title: Uncaught TypeError: Cannot read property 'length' of undefined array
body: <p>I want to create a function to animate text like typewriter. I can't understand why <code>words[index].length</code> is returning undefined in setInterval funcation.</p>
  <pre><code>let speed = Math.floor(Math.random() * 1000); 
  
  function typeWriter() {
      let words = [&quot;Achieve&quot;, &quot;Attain&quot;]; /* The text */ // 6
  
      let typeWriter = document.getElementById(&quot;typewriter&quot;);
      let i = 1;
      let index = 0;
      while (index &lt; words.length) {
  
          setInterval(() =&gt; {
  
              if (i &lt;= words[index].length &amp;&amp; i &gt; 0) {
                  typeWriter.innerHTML = words[index].slice(0, i);
  
                  if (i == words[index].length) {
                      i = -1;
                  }else {
                      i++;
                  }
              } else if ((i * -1) &lt;= words[index].length &amp;&amp; i &lt; 0) {
                  typeWriter.innerHTML = words[index].slice(0, i);
  
                  if ((i * -1) == words[index].length) {
                      clearInterval();
                  }else {
                      i--;
                  }
              }
  
              speed = Math.floor(Math.random() * 1000);
  
         }, speed);
  
         if (index == words.length) {
             index = 0;
         } else {
             index++
         }
      }
  }
  </code></pre>