𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀, 𝗧𝗵𝗿𝗲𝗮𝗱𝘀, 𝗮𝗻𝗱 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻
Understanding JavaScript fundamentals helps you write better code. Here is a breakdown of functions, processes, threads, and execution styles.
𝟱 𝗧𝘆𝗽𝗲𝘀 𝗼𝗳 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀
- Named Function: A function with a specific name. It helps with debugging and reuse.
- Anonymous Function: A function without a name. You often use these as callbacks.
- Function Expression: You assign a function to a variable. You call it using that variable name.
- Arrow Function: A short syntax using the => symbol. It handles 'this' differently.
- IIFE: A function that runs as soon as you define it. It creates a private scope.
𝗣𝗿𝗼𝗰𝗲𝘀𝘀 𝘃𝘀. 𝗧𝗵𝗿𝗲𝗮𝗱
A Process is a program running on your computer.
- It has its own memory.
- It has a unique ID called a PID.
- If one process crashes, others stay running.
- Example: Chrome and Spotify are separate processes.
A Thread is a small unit inside a process.
- Threads share the same memory within a process.
- They allow a program to do many things at once.
- Example: Chrome uses multiple threads to render a page while playing sound.
𝗦𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝘃𝘀. 𝗔𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀
JavaScript is single-threaded. This means it handles one task at a time.
Synchronous Execution:
- Tasks run one after another.
- Each line waits for the previous line to finish.
- A slow task can stop everything else from running.
Asynchronous Execution:
- Tasks can start now and finish later.
- It does not block the rest of your code.
- This works through the Event Loop.
- The browser uses Web APIs to handle tasks like timers or data fetching in the background.
- Once a background task finishes, the Event Loop pushes it back to the stack to run.