
Behind the Scenes of JavaScript: Synchronous vs Asynchronous Explained Simply
What is Synchronous JavaScript Synchronous means code runs line by line, one after another. Next line waits until the previous line finishes. Example: console . log ( " Start " ); function slowTask () { for ( let i = 0 ; i < 1 e9 ; i ++ ) {} // takes time console . log ( " Task Done " ); } slowTask (); console . log ( " End " ); Output : Start Task Done End Explanation: Here, End waits until slowTask finishes. This is Synchronous. What is Asynchronous JavaScript? Asynchronous means code does NOT wait. It runs tasks in background and continues next lines. Example: console . log ( " Start " ); setTimeout (() => { console . log ( " Task Done " ); }, 2000 ); console . log ( " End " ); Output : Start End Task Done Explanation: Here, End does NOT wait → This is Asynchronous. Why Asynchronous is Needed : Because JavaScript is single-threaded (one task at a time). Async helps to handle: API calls Database calls File reading Timers User actions Without async → Website will freeze. Asynchronous
Continue reading on Dev.to Webdev
Opens in a new tab




