Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript TypeError: can't convert undefined to object

var horizont, vertikal = new Array ()

for (var i=0; i < 9; i++) 
{
horizont[i] = new Array ();
vertikal[i] = new Array ()
}

That's what the console told me:

TypeError: can't convert undefined to object

horizont[i] = new Array ();

(if I would erase it from the code he says the same with vertikal )

except from some other empty strings getiing born it's the beginning of my code... where is the mistake? Is it so ovious that I don't see it?

like image 270
Robin93K Avatar asked Aug 12 '13 16:08

Robin93K


People also ask

How do you check if JavaScript object is empty?

Use Object. Object. keys will return an array, which contains the property names of the object. If the length of the array is 0 , then we know that the object is empty.

What is object keys in JavaScript?

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object . The ordering of the properties is the same as that given by looping over the properties of the object manually.


1 Answers

The error is because you did not define horizont as an Array. You are using a comma to separate your variable so it is undefined. It does not use the new Array() from vertikal.

If you take your code

var horizont, vertikal = new Array ()

And write it out to use multiple variable, the error would pop out.

var horizont;
var vertikal = new Array();

You need to specify both as Arrays.

var horizont = [], 
    vertikal = [];
like image 74
epascarello Avatar answered Nov 03 '22 20:11

epascarello