
setTimeout Internals, Anonymous Functions, and the this Keyword — Properly This Time
One of those sessions where something you thought you understood gets explained at a deeper level and suddenly makes way more sense. The this keyword especially — this is the full picture. setTimeout Is Just a Function Too This sounds obvious but it's worth saying clearly. setTimeout is itself a function — one that the browser gives you. Under the hood it looks something like: function setTimeout(callback, delay) { // browser starts the timer // when delay is done, calls callback() } The timer doesn't start on its own. You start it the moment you write: setTimeout(myFunc, 3000); That line is you saying — "hey browser, start a 3 second countdown, then call myFunc." You own the trigger. The browser owns the clock. The Only Two Valid Ways to Write an Anonymous Function There are exactly two ways to define an anonymous function and actually use it: `const myFunc = function() { ... } const myFunc2 = () => { ... }` Both store the function in a variable so you can call it later. Arrow functio
Continue reading on Dev.to Webdev
Opens in a new tab



