const arr = [4, 9, 2, 20, 1, 12];
arr.forEach(num => {
setTimeout(() => console.log(num), num);
});
Output:
1
2
4
9
12
20
Thanks for reading π
Follow @codedrops.tech for daily posts.
Instagram β Facebook
Micro-Learning β Web Development β Javascript β MERN stack
codedrops.tech
Top comments (1)
This ain't a good approach, what if the array has bigger numbers?
Having a sort function inside of the setTimeout is better.