𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀, 𝗣𝗿𝗼𝗰𝗲𝘀𝘀𝗲𝘀, 𝗮𝗻𝗱 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻
Mastering JavaScript requires understanding how code runs.
Here are the core concepts you need to know.
𝟭. 𝟱 𝗧𝘆𝗽𝗲𝘀 𝗼𝗳 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀
- Named Function: Uses a specific name. This helps you debug errors quickly.
- Anonymous Function: Has no name. You use these for callbacks or variable assignments.
- Function Expression: You assign a function to a variable.
- Arrow Function: A short syntax using the => symbol. It handles the this keyword differently.
- IIFE: Runs the moment you define it. It keeps your code isolated.
𝟮. 𝗣𝗿𝗼𝗰𝗲𝘀𝘀 𝘃𝘀. 𝗧𝗵𝗿𝗲𝗮𝗱
A Process is a running program. Each process has its own memory. If Chrome crashes, Spotify keeps running because they are separate processes.
A Thread is a small unit inside a process. Threads share the same memory. This makes them faster and lighter than processes.
𝟯. 𝗦𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝘃𝘀. 𝗔𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀
JavaScript is single-threaded. It usually runs one task at a time.
Synchronous Execution: Tasks run in a strict order. The next line waits for the current line to finish. This can slow down your app if a task takes too long.
Asynchronous Execution: Tasks can start now and finish later. This prevents your code from blocking.
How it works:
- The Call Stack handles your current tasks.
- Web APIs handle long tasks like timers or data requests in the background.
- The Callback Queue holds finished tasks.
- The Event Loop moves tasks from the queue back to the stack when it is empty.
Example of Asynchronous flow:
- Log "Hi"
- Start a timer for 2 seconds
- Log "End"
- (2 seconds pass) Log "Vicky"
The code does not wait for the timer. It moves to "End" immediately.
Optional learning community: https://t.me/GyaanSetuAi