Can the JS array length property ever return a negative value?
No it cannot. MDN says:.
The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power (232).
No.
The spec for the length
property says:
The
length
property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.
There cannot be -1 properties.
Also, and more explicitly, the spec for Array
says:
Every
Array
object has alength
property whose value is always a nonnegative integer less than 232.
Update: Answer still holds for ES2020
Not normally (as other answers have pointed out), but an object that is an instance of an array can have a negative property called length
var b = Object.create([]);
b.length = -1;
alert(b instanceof Array)
alert(b.length);
No, it can't.
Moreover, if you try doing this arr.length = -1
it will throw you an exception