Processing Two-Dimensional Array Elements

I have a TypeError: Cannot read property ‘length’ of undefined. What is the problem second for-loop (grades[row].length)? Any suggestion? I use sublime text for editor.

var grades = [[90, 77, 78], [76, 82, 81], [91, 94, 89]];
var total = 0;
var average = 0.0;
for(var row = 0; row < grades.length; row++){
	for(var col = 0; col < grades[row].length; col++){
		total += grades[row][col];

average = total / grades[row].length;
console.log("Student " + parseInt(row+1) + " average: " + average.toFixed(1));

row does not exist outside of the loop.

average = total / grades[row].length;

At the end of the loop row is equal to 3. So you are trying to access grades[3].length; which does not exist. grades[2].length; is the last element in your array.

row is actually still in scope after the for loop. From docs:

Variables declared with var are not local to the loop, i.e. they are in the same scope the for loop is in. Variables declared with let are local to the statement.

@collinstommy is correct here. The code produces an error because grades[3] references a not existent grades element an evaluates to undefined. undefined values do not have length properties.