🛠️ Phase 4 Complete: The "Input" Component in Pure JS Moving one step closer to a full design system! Today, I successfully implemented a versatile Input Component using only Vanilla JavaScript and CSS. Since I’m building this entire system from scratch before migrating to React, focusing on the core DOM logic for forms has been an invaluable deep dive. Key features of today’s build: 📝 State Handling: Integrated support for default, focus, error, and disabled states. 💡 Helper & Error Text: Built-in logic to toggle between validation messages and helpful hints. ⌨️ Event Integration: Added a onInput callback to handle live data binding without a framework. 🎨 Clean Architecture: Used a modular structure with input.js for logic and input.css for the design tokens. function createInput({ label = "", placeholder = "", value = "", error = "", helperText = "", disabled = false, onInput = null }) { const wrapper = document.createElement("div"); wrapper.classList.add("input-group"); // Label if (label) { const labelEl = document.createElement("label"); labelEl.classList.add("input-label"); labelEl.innerText = label; wrapper.appendChild(labelEl); } // Input const input = document.createElement("input"); input.classList.add("input-field"); input.placeholder = placeholder; input.value = value; input.disabled = disabled; // Error style if (error) { input.classList.add("input-error"); } // Event if (onInput && !disabled) { input.addEventListener("input", (e) => { onInput(e.target.value); }); } wrapper.appendChild(input); // Helper text if (helperText && !error) { const helper = document.createElement("div"); helper.classList.add("input-helper"); helper.innerText = helperText; wrapper.appendChild(helper); } // Error text if (error) { const errorText = document.createElement("div"); errorText.classList.add("input-error-text"); errorText.innerText = error; wrapper.appendChild(errorText); } return wrapper; } function renderInputPage(container) { container.innerHTML = "<h2>Inputs</h2>"; // Normal container.appendChild( createInput({ label: "Username", placeholder: "Enter username", helperText: "This will be visible publicly" }) ); // With value container.appendChild( createInput({ label: "Email", value: "test@mail.com" }) ); // Error container.appendChild( createInput({ label: "Password", placeholder: "Enter password", error: "Password must be at least 6 characters" }) ); ); } The image shows the different variants in action—from standard text fields to validation errors and disabled states. Up next: Modals and Accordions! #JavaScript #WebDevelopment #Frontend #DesignSystems #CodingJourney #VanillaJS #CSS3 #SoftwareEngineering
Implementing Input Component in Vanilla JavaScript
More Relevant Posts
-
JSInterop in Blazor: Is your C#-to-JS bridge suffocating your app? How I vet my JavaScript integrations: ❌ Junior You: The "Chatty Bridge" Trap Request: "We need to render 5,000 coordinates on a JavaScript map interface." Action: Loop through the C# list and send them to JS one by one. #csharp foreach (var coordinate in treeCoordinates) { await JS.InvokeVoidAsync("map.addMarker", coordinate); } The Cost: The C#-to-JS boundary is crossed 5,000 individual times. On Blazor Server, each call is a SignalR round trip. On WASM, each call is a shared memory boundary crossing. The cumulative synchronization overhead freezes the UI thread. The browser gasps for air. You Now: The "Batched Payload" Fix Request: "We need to render 5,000 coordinates on a JavaScript map interface." Action: "Can we package this and cross the bridge exactly once?" #csharp // Cross the boundary once with the entire payload await JS.InvokeVoidAsync("map.addMarkersBatched", treeCoordinatesArray); #javascript // The JS side must handle bulk insertion efficiently too window.map = { addMarkersBatched: function (coordinates) { // Batch DOM operations in one pass; don't loop and mutate individually const markers = coordinates.map(c => createMarker(c.lat, c.lng)); markerLayer.addLayers(markers); } } Important: Batching on the C# side moves the bottleneck, it doesn't eliminate it. If your JS function loops through 5,000 coordinates and mutates the DOM individually, you've just shifted the freeze from the bridge to the browser's render thread. The JS side needs to handle bulk insertion in a single pass. What you gain: →Responsive UI: The boundary crossing overhead drops from 5,000 hits to one; the UI thread stays free. →Server-friendly: On Blazor Server, you go from 5,000 SignalR round trips to one. Your infrastructure will thank you. →Scalable pattern: The same approach applies to any bulk operation across the interop boundary; chart data, table rows, canvas drawing instructions. Treat the Blazor-to-JS bridge like a toll road. Don't pay the toll for every single passenger; put them all in a bus. 💾 Save this for your next map, chart, or data-heavy JS integration. Have you ever frozen a browser tab pushing data to a JS library one record at a time? #Blazor #DotNet #JSInterop #WebPerformance #CleanCode
To view or add a comment, sign in
-
-
I used to manipulate objects directly in JavaScript. Until one day, it didn't work. And the errors were almost impossible to trace. Meanwhile JavaScript has built a clean, deliberate API specifically for the job I had been doing messily for a long time. What is Reflect? Reflect is a built-in JavaScript object that provides a set of methods for performing fundamental operations on objects. The same operations you've always done, but in a more controlled, predictable, and reliable way. Reading properties. Setting values. Checking existence. Deleting keys. Reflect does all of this in a clean way. The most important Reflect methods: -> Reflect.get() - reads a property from an object. const user = { name: "Markus" }; Reflect.get(user, "name"); -> "Markus" Same as user.name - but more explicit and safer in dynamic contexts. -> Reflect.set() - sets a property value and returns true or false. const user = { name: "Markus" }; Reflect.set(user, "name", "John"); -> true console.log(user.name); -> "John" Unlike direct assignment - it tells you whether it succeeded by returning true. -> Reflect.has() - checks if a property exists. Reflect.has(user, "name"); -> true Same as the in operator - but cleaner in functional and dynamic code. -> Reflect.deleteProperty() - deletes a property safely. Reflect.deleteProperty(user, "name"); -> true Same as the delete keyword - but returns a boolean instead of throwing silently. -> Reflect.ownKeys() - returns all keys of an object. const user = { name: "Markus", age: 25}; Reflect.ownKeys(user); -> ["name", "age"] Where Reflect truly shines - with Proxy. Reflect and Proxy are natural partners. Inside a Proxy trap, Reflect lets you perform the default operation in a clean way - without rewriting the behaviour from scratch. Example: const proxy = new Proxy(user, { get(target, key) { console.log(`Reading: ${key}`); return Reflect.get(target, key); -> clean default behaviour } }); Reflect doesn't replace what you already know. It refines it. It makes the operations you perform on objects more intentional, consistent, and significantly easier to debug when something goes wrong.
To view or add a comment, sign in
-
-
#react #bundler #1 **Everything about Parcel** A Parcel bundler is a tool used in React to prepare your code so browsers can understand and run it efficiently. 📦 What is Parcel Bundler? Parcel is a web application bundler. 👉 In simple terms: It takes all your project files (JS, CSS, images, etc.) and combines + optimizes them into fewer files that a browser can load easily. Why do we need Parcel? When you build a React app, you write code like: JSX (not understood by browser) Modern JavaScript (ES6+) Multiple files (components, styles, assets) 👉 Browsers can’t directly understand or efficiently load this. So Parcel helps by: 1. Converts JSX → Browser JS React uses JSX: const App = () => <h1>Hello</h1>; Parcel converts it into plain JavaScript. 2. Combines multiple files Instead of: App.js Header.js Footer.js styles.css 👉 Parcel bundles them into: bundle.js bundle.css 3. Improves performance Minifies code (removes spaces/comments) Compresses files Optimizes images 4. Handles dependencies automatically No need to manually configure everything like older tools. ⚙️ How Parcel Works (Step-by-Step) Step 1: Entry Point You give Parcel a starting file: index.html Step 2: Builds Dependency Graph Parcel scans your code: import Header from "./Header"; 👉 It tracks all imports and connections. Step 3: Transforms Code JSX → JS SCSS → CSS TypeScript → JS (if used) Step 4: Bundling Combines everything into optimized bundles. Step 5: Serve / Build parcel index.html → runs dev server parcel build index.html → production build 🚀 Key Features 🔹 Zero Configuration No need for heavy setup (unlike Webpack) 🔹 Fast Builds Uses caching and parallel processing. 🔹 Hot Module Replacement (HMR) Changes reflect instantly without full reload. 🔹 Supports many file types JS, CSS, images, fonts, etc. 🧠 Simple Analogy Think of Parcel like a factory packaging system: Raw materials = your code files Machine (Parcel) = processes everything Final box = optimized bundle ready for browser 📌 Example in React npm install parcel "scripts": { "start": "parcel index.html", "build": "parcel build index.html" } Run: npm start 👉 Your React app runs with Parcel handling everything. #React #FrontEnd #SoftwareDevelopment
To view or add a comment, sign in
-
How to build a before/after image reveal effect… (Without using JavaScript): 🚀 Ever seen those sliders where you compare two images by dragging? That’s called an image comparison effect, and it’s widely used in: → Photo editing tools → Landing pages → Case studies But here’s the catch: We used to rely heavily on JavaScript for this. --- 💥 What we used before (JavaScript approach): → Track mouse position (`mousemove`) → Calculate width dynamically → Update styles on every frame → Handle drag logic manually Something like this: ```id="jsold1" const container = document.querySelector(".compare"); const afterImage = document.querySelector(".after"); container.addEventListener("mousemove", (e) => { const rect = container.getBoundingClientRect(); const x = e.clientX - rect.left; const percent = (x / rect.width) * 100; afterImage.style.clipPath = `inset(0 ${100 - percent}% 0 0)`; }); ``` 👉 Works… but feels heavy for such a simple UI. --- Now here’s the fun part: We don’t need JavaScript anymore. --- Here’s how to build it using pure CSS: 1️⃣ Stack both images inside a container: The trick is to place one image over the other. ```id="htmlstep1" <div class="compare"> <img src="before.jpg" /> <img src="after.jpg" class="after" /> </div> ``` --- 2️⃣ Make the container relative: This allows the top image to position correctly. ```id="cssstep2" .compare { position: relative; width: 400px; overflow: hidden; } ``` --- 3️⃣ Position the top image: Place it exactly over the first one. ```id="cssstep3" .after { position: absolute; top: 0; left: 0; } ``` --- 4️⃣ Hide it using clip-path: This is where the magic happens. ```id="cssstep4" .after { clip-path: inset(0 100% 0 0); transition: clip-path 0.6s ease; } ``` 👉 This makes the image invisible from the right side. --- 5️⃣ Reveal it on hover: ```id="cssstep5" .compare:hover .after { clip-path: inset(0 0 0 0); } ``` 👉 Now the image smoothly reveals on hover. --- And if you want to go one step ahead, try this: ✨ Show both images at once: ```id="cssstep6" .compare:hover .after { clip-path: inset(0 50% 0 0); } ``` ✨ Add a diagonal reveal: ```id="cssstep7" .after { clip-path: polygon(0 0, 0 0, 0 100%, 0 100%); } .compare:hover .after { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); } ``` --- Anyways: CSS is no longer just styling. It’s handling: → Interactions → Animations → UI behavior 👉 Things we used to depend on JavaScript for. And honestly… Sometimes CSS is all you need. #CSS #WebDevelopment #Frontend #UI #UX
To view or add a comment, sign in
-
-
POST 1 — The JavaScript Event Loop Is Not What You Think ⚙️ Every JavaScript developer says they understand the event loop. Most of them don't. And that gap is exactly why async bugs are so hard to find and fix. Here's what's actually happening 👇 ───────────────────────── First — JavaScript is single-threaded. Always. One thread. One call stack. One thing running at a time. So how does it handle timers, fetch calls, and user events without freezing? It doesn't. The BROWSER does. And then it reports back. ───────────────────────── The pieces most developers mix up: The Call Stack → where your code actually runs. Functions get pushed in, executed, and popped out. This is the only place code runs. Web APIs → setTimeout, fetch, DOM events. These live OUTSIDE the JS engine — in the browser or Node runtime. They run in separate threads you never manage. The Task Queue (Macrotask Queue) → where callbacks from Web APIs wait to be picked up. setTimeout callbacks land here. The Microtask Queue → where Promise callbacks and queueMicrotask calls wait. This queue has HIGHER priority than the task queue. ───────────────────────── The loop itself: 1. Run everything currently on the call stack until it's empty 2. Drain the ENTIRE microtask queue — every single item, including new ones added during draining 3. Pick ONE task from the task queue 4. Go back to step 1 ───────────────────────── Why this produces bugs nobody expects: Promise.resolve().then() runs before setTimeout(() => {}, 0) — always. Microtasks can starve the task queue if you keep adding new ones during draining. Long synchronous code blocks EVERYTHING — no timers fire, no events respond, no UI updates. ───────────────────────── The practical rule: Never put heavy computation directly on the call stack. Break it up. Use setTimeout to yield back to the event loop. Keep microtask chains short and predictable. ───────────────────────── Did the microtask vs task queue distinction surprise you? Drop a comment below 👇 #JavaScript #WebDevelopment #FrontendDevelopment #Programming #WebPerformance
To view or add a comment, sign in
-
Ever tried mixing HTML inside JavaScript… and everything just broke? 🤯 Or got weird errors for something that “looks like valid HTML”? That’s where 𝗝𝗦𝗫 𝗶𝗻 𝗥𝗲𝗮𝗰𝘁 changes the game. 🚀 𝗝𝗦𝗫 𝗕𝗮𝘀𝗶𝗰𝘀 — 𝗪𝗿𝗶𝘁𝗶𝗻𝗴 𝗨𝗜 𝗶𝗻𝘀𝗶𝗱𝗲 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 In React, we don’t write traditional HTML. Instead, we use 𝗝𝗦𝗫 (𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗫𝗠𝗟) — a syntax that lets you write HTML-like code inside JavaScript. 👉 It makes UI code more readable and component-driven. 💡 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗝𝗦𝗫 (𝗮𝗻𝗱 𝘄𝗵𝘆 𝘂𝘀𝗲 𝗶𝘁)? JSX is a syntax extension for JavaScript used in React to describe UI. Under the hood: ◦ JSX gets converted into React.createElement() ◦ It helps React understand what UI to render 👉 Why developers love it: ◦ Cleaner and more readable UI code ◦ Combines logic + UI in one place ◦ Reduces manual DOM manipulation 🔍 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: 𝗦𝗶𝗺𝗽𝗹𝗲 𝗝𝗦𝗫 𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝐺𝑟𝑒𝑒𝑡𝑖𝑛𝑔() { 𝑐𝑜𝑛𝑠𝑡 𝑛𝑎𝑚𝑒 = "𝑆ℎ𝑢𝑏ℎ𝑎𝑚"; 𝑟𝑒𝑡𝑢𝑟𝑛 ( <𝑑𝑖𝑣> <ℎ2>𝐻𝑒𝑙𝑙𝑜, {𝑛𝑎𝑚𝑒} 👋</ℎ2> <𝑝>𝑊𝑒𝑙𝑐𝑜𝑚𝑒 𝑡𝑜 𝑅𝑒𝑎𝑐𝑡 𝐽𝑆𝑋 𝑏𝑎𝑠𝑖𝑐𝑠!</𝑝> </𝑑𝑖𝑣> ); } 🧠 𝗪𝗵𝗮𝘁’𝘀 𝗵𝗮𝗽𝗽𝗲𝗻𝗶𝗻𝗴 𝗵𝗲𝗿𝗲? ◦ {name} → injects JavaScript inside JSX ◦ JSX looks like HTML, but it’s actually JavaScript ◦ The component returns UI that React renders to the DOM 👉 This is declarative UI — you describe what to show, not how to update it ⚠️ 𝗝𝗦𝗫 𝗥𝘂𝗹𝗲𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗠𝘂𝘀𝘁 𝗞𝗻𝗼𝘄 1️⃣ 𝗥𝗲𝘁𝘂𝗿𝗻 𝗮 𝘀𝗶𝗻𝗴𝗹𝗲 𝗽𝗮𝗿𝗲𝗻𝘁 𝗲𝗹𝗲𝗺𝗲𝗻𝘁 𝑟𝑒𝑡𝑢𝑟𝑛 ( <𝑑𝑖𝑣> <ℎ1>𝐻𝑒𝑙𝑙𝑜</ℎ1> <𝑝>𝑊𝑜𝑟𝑙𝑑</𝑝> </𝑑𝑖𝑣> ); 2️⃣ 𝗨𝘀𝗲 𝗰𝗹𝗮𝘀𝘀𝗡𝗮𝗺𝗲 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗰𝗹𝗮𝘀𝘀 <𝑑𝑖𝑣 𝑐𝑙𝑎𝑠𝑠𝑁𝑎𝑚𝑒="𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑒𝑟"></𝑑𝑖𝑣> 3️⃣ 𝗔𝗹𝘄𝗮𝘆𝘀 𝗰𝗹𝗼𝘀𝗲 𝘁𝗮𝗴𝘀 <𝑖𝑚𝑔 𝑠𝑟𝑐="𝑖𝑚𝑎𝑔𝑒.𝑝𝑛𝑔" /> 4️⃣ 𝗨𝘀𝗲 𝗰𝘂𝗿𝗹𝘆 𝗯𝗿𝗮𝗰𝗲𝘀 𝗳𝗼𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 <ℎ1>{2 + 2}</ℎ1> 5️⃣ 𝗜𝗻𝗹𝗶𝗻𝗲 𝘀𝘁𝘆𝗹𝗲𝘀 𝘂𝘀𝗲 𝗼𝗯𝗷𝗲𝗰𝘁𝘀 <𝑑𝑖𝑣 𝑠𝑡𝑦𝑙𝑒={{ 𝑐𝑜𝑙𝑜𝑟: "𝑏𝑙𝑢𝑒", 𝑓𝑜𝑛𝑡𝑆𝑖𝑧𝑒: "18𝑝𝑥" }}></𝑑𝑖𝑣> 🏗️ 𝗥𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝘂𝘀𝗲 𝗰𝗮𝘀𝗲𝘀 JSX is used everywhere in React apps: ◦ Building reusable UI components ◦ Rendering dynamic data (API responses) ◦ Conditional rendering (login/logout UI) ◦ Mapping lists (products, users, posts) 📌 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀 ✔ JSX makes UI code readable and expressive ✔ It combines JavaScript logic with HTML-like syntax ✔ Following JSX rules avoids common beginner mistakes Once you get comfortable with JSX… React starts to feel natural. 💬 Do you find JSX intuitive or confusing when you first learned it? 🚀 Follow Shubham Kumar Raj for more such content. #JavaScript #WebDevelopment #Frontend #CodingTips #100DaysOfCode #codinginterview #learnjavascript #programming #interviewprep #CareerGrowth #SowftwareEngineering #OpenToWork #ReactJS #FrontendDevelopment #Coding
To view or add a comment, sign in
-
-
🚀 setTimeout() vs setInterval() in JavaScript 📘 Definition: setTimeout() is a built-in JavaScript function that executes a given function once after a specified delay (in milliseconds). 👉 Syntax: setTimeout(callbackFunction, delay); 👉 Example: setTimeout(() => { console.log("Runs after 2 seconds"); }, 2000); Key Points: ✔ Executes only once ✔ Delay is in milliseconds (1000ms = 1 second) ✔ Time is not exact, it's a minimum delay 📘 Definition: setInterval() is a built-in JavaScript function that repeatedly executes a function at fixed time intervals. 👉 Syntax: setInterval(callbackFunction, interval); 👉 Example: setInterval(() => { console.log("Runs every 2 seconds"); }, 2000); Key Points: ✔ Executes again and again ✔ Runs at a fixed interval ✔ Continues until manually stopped 🧠 How JavaScript Handles Them (Event Loop Concept) JavaScript is single-threaded, meaning it can execute one task at a time. So how does it handle timers? 👉 Flow: >>>setTimeout / setInterval are handled by Browser APIs >>>After delay, callbacks go to the Callback Queue >>>The Event Loop moves them to the Call Stack when it’s empty That’s why: 👉 Execution time is not guaranteed 👉 It depends on what’s already running ⚠️ Important Difference ✔ setTimeout → Executes once after delay ✔ setInterval → Executes repeatedly at intervals ⚠️ Common Issue with setInterval() If the function takes longer than the interval: >> Calls can overlap >> Performance issues may occur 💡 Better Alternative (Advanced Concept) Using recursive setTimeout() function runTask() { setTimeout(() => { console.log("Controlled execution"); runTask(); }, 2000); } runTask(); ✔ Ensures next execution starts after previous finishes ✔ Gives better control 🛑 Stopping Timers: const id = setInterval(() => { console.log("Running..."); }, 1000); clearInterval(id); Use: ✔ clearTimeout() to stop timeout ✔ clearInterval() to stop interval Real-World Use Cases 🔹 setTimeout(): Delayed popups, API retry logic , Debouncing inputs 🔹 setInterval(): Digital clocks , Live dashboards, Polling servers #JavaScript #AsyncJS #EventLoop #FrontendDevelopment #Coding #WebDevelopment
To view or add a comment, sign in
-
JavaScript has two completely different systems for sharing code between files. some developers use both without knowing which one they're using - or why it is necessary to know. Every JavaScript file you've ever written that shares code with another file, uses a module system. Some developers use them daily without truly understanding what separates one from the other. Though the gap is small. The consequences of it are not. Modules are a way to break your code into separate, reusable pieces rather than writing everything in one enormous file. It makes the code clean, organised and maintainable. There are two main systems for doing this in JavaScript. And they are not the same. CommonJS: - Uses require() to bring in modules and uses module.exports to send them - It loads modules synchronously (blocks execution) - It is mostly used in older Node.js environments Example: -> Exporting module.exports = { greet: () => console.log("Hello") }; -> Importing const myModule = require('./myModule.js'); myModule.greet(); -> "Hello" How CommonJS loads: It loads synchronously - meaning JavaScript stops everything and waits for the module to fully load before moving on. This is Problematic for browsers. ES Modules (ESM): The current standard for browsers and modern Node.js. It is cleaner, more powerful, and built for performance. -> Named export export const greet = () => console.log("Hello"); -> Default export export default function greet() { console.log("Hello"); } -> Importing named import { greet } from './myModule.js'; -> Importing default import greet from './myModule.js'; Dynamic Imports ES Modules loads only what you need, only when you need it. When you hear "you're not splitting," this is what they mean. Instead of loading every module upfront - dynamic imports let you load a module on demand, only when it's actually needed. It results to a faster initial load times. ES Modules import() returns a Promise - so it works with .then() or async/await. It is also supported natively in ESM and available in CJS environments too. ES Modules aren't just a syntax choice. They're an architectural decision. The right system, used the right way, is the difference between an app that loads instantly and one that makes users wait. And users don't like to wait. So if you need your code base to be performant, you have your answer.
To view or add a comment, sign in
-
#js #8 **Why JavaScript Is Single Threaded Synchronous Language** 🧠 Statement: 👉 “JavaScript is single-threaded and synchronous” We’ll break this into parts so it’s easy to understand. 🧵 1. What is Single-Threaded? 👉 JavaScript has only ONE main thread That means: One call stack One task at a time Example: console.log("A"); console.log("B"); console.log("C"); 👉 Output: A B C ✔ It runs one by one, not in parallel. ⚙️ Why JavaScript is single-threaded? Originally designed for browsers. Browsers (like Google Chrome) need to: Update UI Handle clicks 👉 If multiple threads changed UI at same time: UI would break Data would be inconsistent ✔ So JavaScript uses one thread → safe & predictable ⏱️ 2. What is Synchronous? 👉 Synchronous means: Code runs line by line, and each line waits for previous one to finish Example: console.log("Start"); function slowTask() { for (let i = 0; i < 1; i++) {} } slowTask(); console.log("End"); 👉 Output: Start End (after delay) ✔ End waits until slowTask finishes 🔴 Problem with synchronous nature If something takes time: Everything stops ⛔ UI freezes 👉 Bad user experience 🔄 3. Then how does JavaScript handle async? Here’s the important twist 👇 👉 JavaScript is: ✔ Single-threaded ✔ Synchronous (by default) ❗ BUT supports async using system outside JS 🌐 Behind the scenes JavaScript uses: Browser APIs Event system → Event Loop 📦 Example console.log("Start"); setTimeout(() => { console.log("Async"); }, 2000); console.log("End"); 🔄 Execution flow Start → runs setTimeout → sent to browser End → runs immediately After 2 sec → callback comes back Event loop executes it Output: Start End Async 👉 So JS looks async, but actually: It delegates work Still runs one task at a time 🧑🍳 Simple analogy You (JS) 👨🍳: Cook one dish at a time (single-threaded) Follow order (synchronous) Ask assistant to do waiting tasks (async) 👉 JavaScript is: ✔ Single-threaded One task at a time ✔ Synchronous (by default) Executes line by line ✔ Non-blocking (with help) Uses event loop + browser APIs 👉 JavaScript is single-threaded and synchronous, but uses the event loop to handle asynchronous operations without blocking execution. #Javascript #ObjectOrientedProgramming #SoftwareDevelopment
To view or add a comment, sign in
-
𝗧𝗼𝗽𝗶𝗰 𝟭𝟮: 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 (𝗠𝗮𝗰𝗿𝗼 𝘃𝘀 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸𝘀) JavaScript is single-threaded, yet it handles thousands of concurrent operations without breaking a sweat. The secret isn't raw speed; it's an incredibly strict, ruthless prioritization engine. If you don't understand the difference between a macro and a microtask, your "asynchronous" code is a ticking time bomb for UI freezes. Summary: The Event Loop is the conductor of JavaScript's concurrency model. It continuously monitors the Call Stack and the Task Queues. To manage asynchronous work, it uses two distinct queues with vastly different priorities: Macrotasks (like setTimeout, setInterval, network events) and Microtasks (like Promises and MutationObserver). Crux: The VIP Queue: Microtasks have absolute priority. The engine will not move to the next Macrotask, nor will it allow the browser to re-render the screen, until the entire Microtask queue is completely empty. The Normal Queue: Macrotasks execute one by one. After a single Macrotask finishes, the Event Loop checks the Microtask queue again. The Starvation Risk: Because Microtasks can spawn other Microtasks, a recursive Promise chain can hold the main thread hostage, permanently blocking the UI from updating. The Deep Insight (Architect's Perspective): Traffic Control and Yielding: As architects, we must visualize the Event Loop as a traffic control system where Microtasks are emergency vehicles. When you resolve a massive chain of Promises or heavy async/await logic, you are flooding the intersection with ambulances. The browser's rendering engine—the normal traffic—is forced to sit at a red light until every single emergency vehicle has cleared. We don't just use async patterns for code readability; we must actively manage thread occupancy. For heavy client-side computations, we must intentionally interleave Macrotasks (like setTimeout(..., 0)) to force our code to yield control back to the Event Loop, allowing the browser to paint frames and keeping the UI responsive. Tip: If you have to process a massive dataset on the frontend (e.g., parsing a huge JSON file or formatting thousands of rows), do not use Promise.all on the entire set at once. That floods the Microtask queue and locks the UI. Instead, "chunk" the array and process each chunk using setTimeout or requestAnimationFrame. This gives the Event Loop room to breathe and the browser a chance to render 60 frames per second between your computation chunks. Tags: #SoftwareArchitecture #JavaScript #WebPerformance #EventLoop #FrontendEngineering #CleanCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development