javascript array.sort with undefined values

Yes, you can safely assume undefined will get moved to the end of the array.

From MDC:

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array

From the spec, 15.4.4.11
:

Because non-existent property values always compare greater than undefined property values, and
undefined always compares greater than any other value, undefined property values always sort to the end of the result,
followed by non-existent property values.

Leave a Comment