I understand that once the game ends, after 500ms, I would be displaying the alert message. But can someone explain to me why do I need to include an arrow function?
Why cant it be written like this?
setTimeout({`Game Over! Your total score is ${totalScore}`); 500);
As said it takes a function, more specifically a callback function (it can also take a code string).
All setTimeout does is invoke the callback. The callback can have whatever code you need to run with a timeout. In this case, the setTimeout callback will be used to call two other functions, alert and resetGame.
Also, passing data directly to setTimeout wouldn’t make sense, I mean what would it do with it? It is just a function used to delay some execution.
It is a standard API design because it is versatile. A function can do anything, contain a lot of code and data, and interact with the outside. If all the API accepted was data it would be a lot more constrained in how and what it was used for.