Using the < or > operators to compare an array to a number is not safe, reliable or correct. You should not do it.
Here are the conversion rules taken from this O'Reilly book:
The operands of these comparison operators may be of any type.
Comparison can be performed only on numbers and strings, however, so
operands that are not numbers or strings are converted. Comparison and
conversion occur as follows:
If both operands are numbers, or if both convert to numbers, they are
compared numerically.
If both operands are strings or convert to strings, they are compared
as strings.
If one operand is or converts to a string and one is or converts to a
number, the operator attempts to convert the string to a number and
perform a numerical comparison. If the string does not represent a
number, it converts to NaN, and the comparison is false. (In
JavaScript 1.1, the string-to-number conversion causes an error
instead of yielding NaN.)
If an object can be converted to either a number or a string,
JavaScript performs the numerical conversion. This means, for example,
that Date objects are compared numerically, and it is meaningful to
compare two dates to see whether one is earlier than the other.
If the operands of the comparison operators cannot both be
successfully converted to numbers or to strings, these operators
always return false.
If either operand is or converts to NaN, the comparison operator
always yields false.
In this particular case, since there is no conversion of an array to a number, it ends up comparing two strings which is completely unreliable.
This is yet another reason why you should only compare two items of the same type. In this particular case, it's also much, much more readable code to use the .length property directly and use:
a.length > 2
array.lengthisn't always safe either considering that it doesn't always readjust when an element is deleted ;).lengthalways represents the actual length of the array. If an element is removed with something like.splice()or.pop(), then.lengthwill immediately and reliably return the new length. If you merely set an array element to undefined, that array element still exists and all the other array elements stay in their same locations so the.lengthvalue correctly does not change. Can you describe when you think.lengthis not accurate.deletingelements at the end of the array will not adjust thelengthproperty (perhaps intentionally), and there has been browser discrepancies in the past (hexmen.com/blog/2006/12/push-and-pop). In addition, it is way too easy to change the property itself, and a fallacious framework or lazy programmer can cause a lot of confusion..lengthproperty? And, what do you recommend the OP do to solve their question.