Cannot read property '0' of undefined in jquery
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;meta charset="ISO-8859-1"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;style&gt;
td
{
    min-height: 50px;
    min-width: 50px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border="1" id="tab"&gt;
&lt;tr&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
    &lt;td class="game"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;script&gt;
    var tab=[4];
    $(document).ready(function()
    {
        var i,j;
        for(i=0;i&lt;4;i++)
        {
            tab[i]=[4];
            for(j=0;j&lt;4;j++)
            {
                tab[i][j]=null;
            }
        }
        randomnum();
    });
    function randomnum()
    {
        var num=Math.random();
        alert("called random num");
        if(num&lt;0.5)
            num=2;
        else
            num=4;
        alert(num);
        var row=Math.floor(Math.random()*10);
        row=row%4;
        var col=Math.floor(Math.random()*10);
        col=col%4;
        while(tab[row][col]!=null)
        {
            var row=Math.random();
            row=(row*10)%4;
            var col=math.random();
            col=(col*10)%4;
            alert("random row col"+row+" "+col);
        }
        //alert("row:"+row+"col"+col);
        tab[row][col]=num;
        $("#tab tr:eq("+row+") td:eq("+col+")").text(num);
        keycheck();
    }
    function keycheck()
    {
        $(document).on("keydown",function(event){
            if(event.which==38)
                moveup();
            else if(event.which==40)
                movedown();
            else if(event.which==39)
                moveright();
            else if(event.which==37)
                moveleft();
            });
        }
    function moveup()
    {
        var row,col,j;
        for(col=0;col&lt;4;col++)
        {
            for(row=0;row&lt;3;row++)
            {
                if(tab[row][col]==tab[row+1][col])
                {
                    tab[row][col]=tab[row][col]*2;
                    row++;
                    tab[row][col]=null;
                }
            }
            for(row=0;row&lt;3;row++)
            {
                for(j=row+1;j&lt;4;j++)
                {
                    if (typeof j === "undefined") {
                        alert("j is undefined");
                    }
                    if (typeof row === "undefined") {
                        alert("col is undefined");
                    }
                    if (typeof col === "undefined") {
                        alert("col is undefined");
                    }
                    alert(j+" "+row+" "+col);
                    if(tab[j][col]==null&amp;&amp;tab[j+1][col]!=null)
                    {
                        tab[j][col]=tab[j+1][col];
                        tab[j+1][col]=null;
                        j++;
                    }
                }
            }
        }
        chntable();
    }
    function chntable()
    {
        var row,col;
        for(row=0;row&lt;4;row++)
        {
            for(col=0;col&lt;4;col++)
            {
                $("#tab tr:eq("+row+") td:eq("+col+")").text(num);
            }
        }
        randomnum();
    }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>in the above code i get the </p>

<blockquote>
  <p><code>Cannot read property '0' of undefined</code></p>
</blockquote>

<p>at <code>moveup()</code> in <code>if(tab[j+1][col]==null&amp;&amp;tab[j][col]!=null)</code>. what is the error and how to solve it? from the alert messages, I could see that none are undefined. So what is triggering the problem? What is the cause of the problem, so I can avoid it in the future? What is given in duplicate is to create an array, although it did not solve my problem. The error in the jquery library is at <code>dispatch</code> and <code>q.handle</code>.</p>