JavaScript is a single threaded programming language used for web development. It supports synchronous and asynchronous execution. In synchronous execution, statements run one after another, and each task waits for the previous one to finish. In asynchronous execution, some tasks run in the background without stopping the execution of other code.
Synchronous JavaScript
Synchronous programming executes code in sequence, where each statement waits for the previous one to complete before running. This makes the flow simple and predictable.
In the above code, “Hi” is logged first, then “Geek”, and finally “How are you”. Each statement executes only after the previous one completes.
Asynchronous JavaScript
Asynchronous programming, on the other hand, allows multiple tasks to run independently of each other. In asynchronous code, a task can be initiated, and while waiting for it to complete, other tasks can proceed. This non-blocking nature helps improve performance and responsiveness, especially in web applications.
- Call Stack: Functions are executed in the order they are called. Each function is added to the stack and completed before the next one runs.
- Web APIs: Functions like setTimeout, HTTP requests, and event listeners are handled by browser Web APIs without blocking the call stack.
- Callback Queue: After a Web API completes its task, the callback function is moved to the callback queue.
- Event Loop: The event loop checks the call stack continuously. If it is empty, it moves functions from the callback queue to the stack for execution.
So, what the code does is first it logs in Hi then rather than executing the setTimeout function it logs in End and then it runs the setTimeout function.
At first, as usual, the Hi statement got logged in. As we use browsers to run JavaScript, there are the web APIs that handle these things for users. So, what JavaScript does is, it passes the setTimeout function in such web API and then we keep on running our code as usual. So it does not block the rest of the code from executing and after all the code its execution, it gets pushed to the call stack and then finally gets executed. This is what happens in asynchronous JavaScript.
Difference between Synchronous and Asynchronous JavaScript
Process vs Thread
Modern operating systems are designed to handle multiple tasks efficiently while maintaining good performance and responsiveness. These are mainly achieved through processes and threads.
Process is the Independent program with its own memory
Thread is small unit of a process sharing same memory
Process
Process is a program that is currently in execution within an operating system. It operates in an independent environment and is managed by the OS for proper scheduling and execution. Processes form the basis of program execution in a multitasking system. Its Properties are:
- Each process has a unique Process ID (PID) for identification.
- Every process moves through different states such as new, ready, running, waiting, and terminated.
- Processes communicate with each other using Inter-Process Communication (IPC) methods.
Thread
Thread is a smallest unit of execution within a process. It enables a program to perform multiple tasks concurrently while sharing the same memory and resources. Threads improve application performance and responsiveness in multitasking environments. Its properties are:
- Each thread has its own Thread ID (TID) for identification.
- A thread also moves through states such as new, runnable, running, waiting, and terminated.
- Threads within the same process share memory and resources, enabling faster communication.
- Context switching can occur between threads to allow multiple tasks to execute efficiently.
Similarities Between Threads and Processes
Units of Execution: Both are execution units within an operating system and are part of process management.
OS Scheduling & Preemption: Both are scheduled by the operating system for fair CPU allocation, and can be preempted for multitasking.
Own Execution Context: Each has its own execution context, including program counter, CPU registers, and stack space.
Creation During Runtime: Both can create child entities during their execution lifecycle.
Communication & Resource Release: Both can communicate using IPC mechanisms, and upon termination, their allocated resources are released back to the operating system
Process vs Thread







Top comments (0)