I was implementing a routing algorithm in javascript, but when I assign a negative one variable in the array gives me this error: invalid array length.
var node = new Array()
node[0] = new Array(6,7)
node[1] = new Array(5,-4,8)
node[2] = new Array(-2) //Here, invalid array length
I do not know how to resolve this error.
If you are trying to initialize an array that contains only a negative number, use the literal syntax:
The problem with the
Arrayconstructor is that when it is invoked only with only one argument, this number is used as thelengthof the new array, e.g.:I recommend you to stick with the literal syntax to avoid those ambiguities.