Soo I didn't found the right explaination, let's go.
JavaScript is asynchronous. Which means a block of code will not wait for the previous to be achieved (if it doesn't needs it's value). In your case, setTimeout is a operation that can be time consuming, and JavaScript will not wait for the end of it before running next loop.
From time to time you need to wait for the execution of a block, best example whould be a web request, like an API call. To work this way js has Promises, which are basically a placeholder saying "I don't have the value yet, but wait for me, here it comes". Promises implement the method then()
(old) or can be await
. Either way, theses tools allows you to use JavaScript synchronously, and the next line will wait for the promise to resolve or fail.
MDN doc on asynchronous and Promise object.