Menu

1 / 2

Synchronous vs Asynchronous JavaScript

DEV Community·Pratham·6 months ago
#osGX84T2
#part#synchronous#console#code#user
Reading 0:00
15s threshold

Why JavaScript doesn't just wait around — and how that changes everything about how you write code.

Let me tell you about the moment asynchronous JavaScript broke my brain.

I wrote this code, fully expecting it to print in order:

console . log ( " First " ); setTimeout (() => console . log ( " Second " ), 0 ); console . log ( " Third " );

Enter fullscreen mode

Exit fullscreen mode

I expected: First → Second → Third.

I got: First → Third → Second.

Zero milliseconds of delay, and "Second" still printed last. I stared at my screen thinking JavaScript was broken. It wasn't. I just didn't understand how JavaScript handles time.

This is the story of synchronous vs asynchronous code, and once you get it, a massive amount of JavaScript behavior suddenly makes sense.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.