How sorting in JavaScript works

A direct answer to this question is "it depends". This is because JavaScript is actually a specification. The actual implementation of sorting depends on the JavaScript engine.

For example, Google's open source V8 engine (used in Chrome and Node.js) implements the specification in a particular way. It uses Tim sort.

But what does the specification say about the behavior exactly? The Array.prototype.sort specification specifies the algorithm.

References