Javascript's setTimeout can sort an array, how?

Shravan Kumar B - Aug 11 '20 - - Dev Community

setTimeout function of the Web API's with Javascript; can sort an array.

Example:


[111,20,13,4,544,32,12,414,123].forEach((item) => {

   setTimeout(()=> console.log(item), item);

});
Enter fullscreen mode Exit fullscreen mode

The same works for negative as well,
Example:

let arr = [10, -100, 650, -25, 5, -50];

const min = -Math.min(...arr)

arr.forEach((item) => {
  setTimeout(() => console.log(item), item + min);
});

Enter fullscreen mode Exit fullscreen mode

Any guesses how?
Of course, not an efficient way to do it.

Clue: Call-Stack and Web API's

. . . . . . . . . . . . . . .
Terabox Video Player