Default Behavior of Javascript
JavaScript is a single-threaded, non-blocking language that can handle multiple tasks efficiently using its event loop. In this blog, we will dive deep into JavaScript's execution model, event loop, and how it processes multiple tasks simultaneously. ๐ฅ
1. JavaScript's Single-Threaded Nature ๐งตโ
JavaScript runs on a single thread, meaning it can execute only one task at a time in the call stack. Unlike multi-threaded languages, where multiple tasks run in parallel, JavaScript processes tasks one by one. This design makes JavaScript efficient but also introduces challenges when handling long-running tasks. ๐โโ๏ธ
Example of Synchronous Execution
console.log("Start");
console.log("Processing...");
console.log("End");
๐ก The output will always be:
Start
Processing...
End
Each statement executes in order because JavaScript follows a synchronous execution model by default.
2. Handling Multiple Tasks Simultaneouslyโ
Even though JavaScript is single-threaded, it can handle multiple operations at the same time using asynchronous programming. This is done using:
๐ท๏ธ Callbacks
โ
Promises
โณ Async/Await
๐Web APIs
Example of Asynchronous Execution
console.log("Start");
setTimeout(() => {
console.log("Async task completed!");
}, 2000);
console.log("End");
๐ก The output will be:
Start
End
Async task completed!
The setTimeout function moves to the Web API environment and executes after 2 seconds, without blocking other synchronous tasks. This is where the event loop comes in! ๐