I got confused when I read that when I set the (same) time in 2 timeSetout methods, the order in which the function will be called cannot always be predicted (there was not reasoning provided).
setTimeout(()=> console.log("first"), 5000);
setTimeout(()=> console.log("second"), 5000);
As I understand, the timesetouts will be added to the event table, which keeps track of the events (in this case the event is the time), then it sends it to the event queue, which stores the order in which the functions should be executed, then event loop checks and sends those functions to the call stack.
Since the first timesetout (logging "first") is being added FIRST to the event table, I would imagine it being the FIRST to be send to the event queue (also considering the event queue is a stak data structure) and the FIRST to be send to the call stack during the event loop.
I couldnt find anything about that online, also not reasons as to why it could be executed in random order and would be happy about an answer!
Also, on a side note: Could someone please explain when race conditions could be possible as if the above is not true, I cannot think of any case since javascript is single threaded. Thanks!!