We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

Our volunteers haven't translated this article into עברית yet. Join us and help get the job done!
You can also read the article in English (US).


TypeError: invalid Array.prototype.sort argument (Firefox)

Error type


What went wrong?

The argument of Array.prototype.sort() is expected to be either undefined or a function which compares its operands.


Invalid cases

[1, 3, 2].sort(5);  // TypeError

var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key] || 'asc');  // TypeError

Valid cases

[1, 3, 2].sort();   // [1, 2, 3]

var cmp = { asc: (x, y) => x >= y, dsc: (x, y) => x <= y };
[1, 3, 2].sort(cmp[this.key || 'asc']); // [1, 2, 3]

See also

Document Tags and Contributors

 Contributors to this page: nmve, fscholz, nbp
 Last updated by: nmve,