25

Would there ever be an instance when the JavaScript array length property returns a negative value? I'm assuming the answer is no, but I was wondering if there would ever be a need to account for negative values when comparing the length of an array in an if statement, for example.

var x = y.length; if (x === 0) { return false; } else if (x > 0) { return true; } else alert("error"); // is this necessary? } 
4
  • 1
    I would think it would definitely never be negative. But could it be undefined? Commented Sep 3, 2015 at 22:16
  • 1
    @xdhmoore no, it can only be a natural number (0 or greater). Commented Sep 3, 2015 at 22:20
  • Can you think of an example of how the .length could possibly be negative? Commented Sep 3, 2015 at 22:21
  • 2
    How certain are you that y is an array? It could be some other kind of object, with or without a .length property. Commented Sep 3, 2015 at 22:21

5 Answers 5

27

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 a length property whose value is always a nonnegative integer less than 232.

Update: Answer still holds for ES2020

Sign up to request clarification or add additional context in comments.

7 Comments

I had read on w3schools: "Return Value: A Number, representing the number of elements in the array object", which isn't as explicit as your source. This is what lead to my question.
@UncleSlug—w3schools is a very ordinary site, do not trust it.
BTW, the current standard is ECMA-262 ed 6 aka ECMAScript 2015. ;-)
I am not quibbling with your fine answer, but I want to point out that the first quoted sentence from the spec is shockingly vague. (1) It does not state what the length value is supposed to be if the Array object is empty (that is, if it has no deletable property). What value is numerically greater than something that does not exist? (2) It does not state that (for non-empty Arrays) the length must be "numerically 1 more than the highest deletable property whose name is an array index". It just states that length must be higher than any index.
@cybersam—unfortunately, it's not all in one place. There is also "Every Array object has a length property whose value is always a nonnegative integer less than 2^32." in ECMASCript 2015 §9.4.2. And yes, the length isn't necessarily always 1 greater, it's just at least one greater.
|
8

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); 

3 Comments

Although, Array.isArray(b) == false
This highlights a weakness of instanceof that has been long known and why it's not used to test whether an object is an instance of something. ;-)
yes for both comments. serves as a reminder that the nicer looking check is not always the best :-)
4

No, it can't. Moreover, if you try doing this arr.length = -1 it will throw you an exception

Comments

2

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).

Comments

0

As said here on Mozilla Developer site (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length), "the length property represents an unsigned, 32-bit integer that specifies the number of elements in an array.". As you can see, it's an unsigned integer of 32 bits. If it's unsigned, then it cannot be negative.

1 Comment

Check again, clear your cache. Latest update was, oh, 30 seconds before my comment. ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.