Sitemap

Difference Between simple code and setTimeout(…code, 0)

2 min readMay 14, 2025

--

If you’re learning JavaScript, you might ask:

“Both print output. So what’s the difference?”

Let me share a quick story before we dive in.

This is a real story of me from work.

A while ago, I was working on a debugging task with one of my colleagues. While discussing how async code works in JavaScript, we reached a small code snippet that used setTimeout() with 0.

They confidently said:

“There’s no difference between console.log('output') and setTimeout(() => { console.log('output') }, 0) — both do the same thing. and doesn’t make sense if used setTimeout with 0”

I disagreed.

So I tested it out, ran a few lines of code, and the result surprised them. and now it’s something I always explain to new developers.

Now, Let’s understand that code line by line.

What does console.log('output') do?

console.log('output');

This runs immediately, line by line.

Output in console

output

JavaScript reads your code from top to bottom, and console.log() runs as soon as it reaches that line.

This is synchronous code — nothing waits, nothing gets delayed.

Now, to the important part we have been waiting for…

What does setTimeout(..., 0) do?

setTimeout(() => {
console.log('output');
}, 0);

This might look like it will run right away because the delay is set to 0, but it actually runs later.

It’s asynchronous — meaning it’s scheduled to run after all the regular code finishes. It goes into the task queue and waits until the JavaScript engine is free.

Read that again…

Get Sangwin Gawande’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s take a contributed code example

console.log('A');

setTimeout(() => {
console.log('B');
}, 0);

console.log('C');

What do you think it prints? Take a pause and try to guess the output here.

A
C
B
  • console.log('A') runs first.
  • console.log('C') runs right after.
  • console.log('B') is delayed — it only runs after the current code finishes.

Wait, What? How? Why Does This Happen?

Here’s how…

JavaScript uses something called the Event Loop.

Here’s a simple way to think of it:

  • Code like console.log('A') and console.log('C') runs immediately.
  • Code like setTimeout(() => { ... }, 0) goes into a waiting line.
  • Once the main code finishes, the event loop says: “Okay, now it’s your turn.”

So even if the delay is 0ms, JavaScript still waits until everything else is done.

Conclusion :

That short argument with my colleague reminded me how important it is to not just assume code will behave the way it looks.

JavaScript’s asynchronous behavior can be tricky, especially for beginners.

So the next time you see setTimeout(..., 0), remember:
It doesn’t run “right now” — it runs after the current code and any other pending microtasks (like Promises).

Learn more about Event Loop here.

Happy Coding!!!

—

Hope this helps.

Cheers!!

Sangwin Gawande

About me : https://sangw.in

--

--

Sangwin Gawande
Sangwin Gawande

Written by Sangwin Gawande

Work with me? Sure, Let's catch up. Website : https://sangw.in Email : imsangwin@gmail.com