🟨 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗗𝗮𝘆 𝟰𝟵: 𝗜𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 𝗼𝗳 𝗖𝗼𝗻𝘁𝗿𝗼𝗹 (𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺) In JavaScript, we often use callbacks to handle async tasks — API calls, timers, or events. But callbacks introduce a hidden issue called Inversion of Control. 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸? A callback is a function passed to another function and executed later. 𝘴𝘦𝘵𝘛𝘪𝘮𝘦𝘰𝘶𝘵(() => { 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘭𝘰𝘨("𝘑𝘢𝘷𝘢𝘚𝘤𝘳𝘪𝘱𝘵"); }, 1000); 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗜𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 𝗼𝗳 𝗖𝗼𝗻𝘁𝗿𝗼𝗹? When we pass a callback, we give control of our code to another function. We don’t control: • When it runs • How many times it runs • Whether it runs at all 🔹 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 𝘢𝘱𝘪.𝘤𝘳𝘦𝘢𝘵𝘦𝘖𝘳𝘥𝘦𝘳(𝘤𝘢𝘳𝘵, 𝘧𝘶𝘯𝘤𝘵𝘪𝘰𝘯 () { 𝘢𝘱𝘪.𝘱𝘳𝘰𝘤𝘦𝘦𝘥𝘛𝘰𝘗𝘢𝘺𝘮𝘦𝘯𝘵(); }); Here: • proceedToPayment is our code • createOrder decides when it runs • Our callback executes only when the API decides, based on its internal logic or async events. This is Inversion of Control in action. 🔹 𝗪𝗵𝘆 𝗧𝗵𝗶𝘀 𝗜𝘀 𝗮 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 • Unexpected behavior • Harder debugging • Leads to callback hell in large apps 🔹 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻 Promises and async / await keep control in our hands. 🔹 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Callbacks give control away. Promises bring it back. 💬 GitHub link in the comments for examples #JavaScript #Day49 #100DaysOfCode #Frontend
Understanding Callbacks and Inversion of Control in JavaScript
More Relevant Posts
-
⚡ JavaScript Event Loop — The Concept That Makes JS Feel “Fast.” Ever wondered how JavaScript handles multiple tasks even though it’s single-threaded? Here are the key things to understand: 🧩 Call Stack Runs your code line by line (one task at a time). 🌐 Web APIs (Browser) Handles slow tasks like setTimeout, fetch, DOM events, etc. 📥 Callback Queue (Task Queue) Stores callbacks waiting to run after the stack is empty. ⚡ Job Queue (Microtask Queue) Promises go here — and it runs before the callback queue ✅ 🔁 Event Loop Continuously checks if the call stack is empty, then pushes queued tasks back to execution. Understanding this helps you: ✅ predict async output order ✅ fix “why is this logging first?” confusion ✅ write better Promise/async-await code ✅ understand sequence vs parallel vs race I wrote a beginner-friendly breakdown with examples. Link in the comments 👇 #JavaScript #WebDevelopment #Frontend #Programming #LearnJavaScript #SoftwareEngineering #Async #EventLoop
To view or add a comment, sign in
-
-
💡 Sunday Dev Tip: JavaScript Array Methods Stop writing loops. Use array methods instead! ❌ Traditional Loop: let doubled = []; for (let i = 0; i < numbers.length; i++) { doubled.push(numbers[i] * 2); } ✅ Modern Approach: const doubled = numbers.map(n => n * 2); Master These Methods: → .map() - Transform each element → .filter() - Keep elements that match → .reduce() - Calculate single value → .find() - Get first match → .some() / .every() - Test conditions Your code becomes: ✅ More readable ✅ Less error-prone ✅ Easier to maintain ✅ More functional Which array method do you use most? 💬 #JavaScript #CleanCode #WebDevelopment #CodingTips #ES6
To view or add a comment, sign in
-
Day 56/100 – JavaScript Closures (Mind = Blown 🤯) Today I finally understood something that used to scare me: 👉 Closures in JavaScript At first, the word itself sounded complicated. But when I broke it down… it actually made sense. Here’s what I learned: A closure happens when a function remembers the variables from its outer scope, even after the outer function has finished executing. Read that again slowly. It means JavaScript doesn’t “forget” everything when a function ends. Example mindset: One function creates a variable. Another function inside it uses that variable. Even after the outer function is done… the inner function still remembers it. That’s powerful. Why this matters: ✔️ Helps in data privacy ✔️ Useful in counters ✔️ Important for callbacks ✔️ Used heavily in real-world applications Big realization today: JavaScript isn’t just about writing code. It’s about understanding how memory and scope actually work. Some concepts take time. But once it clicks… it feels amazing. Slowly becoming more confident with the fundamentals 💙 #100DaysOfCode #JavaScript #Closures #WebDevelopment #LearningInPublic #FrontendJourney
To view or add a comment, sign in
-
-
A lot of developers think 𝗰𝗼𝗻𝘀𝗼𝗹𝗲.𝗹𝗼𝗴, 𝗳𝗲𝘁𝗰𝗵, 𝘀𝗲𝘁𝗧𝗶𝗺𝗲𝗼𝘂𝘁 are part of JavaScript. 𝗧𝗵𝗲𝘆’𝗿𝗲 𝗻𝗼𝘁. They’re 𝗪𝗲𝗯 𝗔𝗣𝗜𝘀 — provided by the browser (or runtime) to the 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗲𝗻𝗴𝗶𝗻𝗲. JavaScript itself? It’s a 𝘀𝗶𝗻𝗴𝗹𝗲-𝘁𝗵𝗿𝗲𝗮𝗱𝗲𝗱, 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 language. 𝗧𝗶𝗺𝗲, 𝗧𝗶𝗱𝗲 𝗮𝗻𝗱 𝗝𝗮𝘃𝗮𝘀𝗰𝗿𝗶𝗽𝘁 𝘄𝗮𝗶𝘁 𝗳𝗼𝗿 𝗻𝗼𝗻𝗲 - A Wise Man (Idk who said it but I'm sure he was wise) So how does async work? 👇 • The JS engine executes code line by line. • When it hits 𝗳𝗲𝘁𝗰𝗵 𝗼𝗿 𝘀𝗲𝘁𝗧𝗶𝗺𝗲𝗼𝘂𝘁, 𝘁𝗵𝗲 𝗯𝗿𝗼𝘄𝘀𝗲𝗿 𝘁𝗮𝗸𝗲𝘀 𝗼𝘃𝗲𝗿. • Once the task completes, the result is pushed to the event loop - can be the 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲 𝗼𝗿 𝘁𝗵𝗲 𝗠𝗮𝗰𝗿𝗼. • The callback/promise gets executed when the 𝗰𝗮𝗹𝗹 𝘀𝘁𝗮𝗰𝗸 𝗶𝘀 𝗳𝗿𝗲𝗲. So technically… JavaScript isn’t “naturally asynchronous” It’s synchronous — powered by async capabilities from its environment. 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗿𝘂𝗻𝘁𝗶𝗺𝗲𝘀 = 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗔𝗣𝗜𝘀. Browser ≠ Node ≠ Deno A few more 𝗪𝗲𝗯 𝗔𝗣𝗜𝘀 we use often - 𝘥𝘰𝘤𝘶𝘮𝘦𝘯𝘵, 𝘸𝘪𝘯𝘥𝘰𝘸, 𝘩𝘪𝘴𝘵𝘰𝘳𝘺, 𝘭𝘰𝘤𝘢𝘵𝘪𝘰𝘯, 𝘭𝘰𝘤𝘢𝘭𝘚𝘵𝘰𝘳𝘢𝘨𝘦, 𝘴𝘦𝘴𝘴𝘪𝘰𝘯𝘚𝘵𝘰𝘳𝘢𝘨𝘦, 𝘯𝘢𝘷𝘪𝘨𝘢𝘵𝘰𝘳 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘦𝘳𝘳𝘰𝘳 (𝘤𝘰𝘯𝘴𝘰𝘭𝘦 𝘪𝘴 𝘵𝘩e 𝘈𝘗𝘐, 𝘦𝘳𝘳𝘰𝘳() 𝘪𝘴 𝘵𝘩𝘦 𝘮𝘦𝘵𝘩𝘰𝘥), 𝘤𝘰𝘯𝘴𝘰𝘭𝘦.𝘥𝘪𝘳 𝘧𝘴, 𝘱𝘳𝘰𝘤𝘦𝘴𝘴, 𝘩𝘵𝘵𝘱, 𝘦𝘵𝘤. #JavaScript #WebDevelopment #EventLoop #SoftwareEngineering
To view or add a comment, sign in
-
Why JavaScript doesn't crash when you call a function before defining it. 🧠 I recently dove deep into the "Execution Context" of JavaScript, and the concept of Hoisting finally clicked. If you’ve ever wondered why this code works: greet(); function greet() { console.log("Hello LinkedIn!"); } ...the answer lies in how the JS Engine treats your code before it even runs a single line. The Two-Phase Secret: Memory Creation Phase: Before the "Thread of Execution" starts, JavaScript scans your code and allocates memory for variables and functions. Functions are stored in their entirety in the Variable Environment. Variables (var) are stored as undefined. Code Execution Phase: Now, the engine runs the code line-by-line. Because the function is already sitting in the memory component, calling it on line 1 is no problem! The Key Takeaway: Hoisting isn't "moving code to the top" (that’s a common myth). It’s actually the result of the Memory Creation Phase setting aside space for your declarations before execution starts. Understanding the "how" behind the "what" makes debugging so much easier. #JavaScript #WebDevelopment #CodingTips #Hoisting #ProgrammingConcepts
To view or add a comment, sign in
-
-
Most developers reach for IDs, classes, or extra state before remembering this: JavaScript already gives you a clean way to attach structured metadata directly to DOM elements. If you have ever used data-* attributes in HTML, you can access them in JavaScript through the dataset property. No parsing. No brittle string manipulation. No unnecessary DOM lookups. It is simple, readable, and surprisingly underused. This pattern is especially useful for event delegation, lightweight UI state, dynamic lists, and keeping behaviour close to the element it belongs to. Small details like this make frontend systems cleaner and easier to reason about without introducing extra abstractions. Not everything needs global state. Sometimes the platform already solved the problem. In the example attached, notice how `data-user-id` becomes `dataset.userId`. Hyphenated attributes automatically convert to camelCase. It is a small feature, but small features compound into cleaner, more maintainable frontend code. What is one underrated JavaScript feature you think more developers should use? Github Gist: https://lnkd.in/d6pjMP7J #webdevelopment #javascript #cleancode
To view or add a comment, sign in
-
-
𝐓𝐨𝐝𝐚𝐲 𝐈 𝐫𝐞𝐯𝐢𝐬𝐢𝐭𝐞𝐝 𝐚 𝐜𝐨𝐫𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐜𝐨𝐧𝐜𝐞𝐩𝐭 𝐭𝐡𝐚𝐭 𝐪𝐮𝐢𝐞𝐭𝐥𝐲 𝐚𝐟𝐟𝐞𝐜𝐭𝐬 𝐜𝐨𝐝𝐞 𝐪𝐮𝐚𝐥𝐢𝐭𝐲: 𝒗𝒂𝒓 𝒗𝒔 𝒍𝒆𝒕 𝒗𝒔 𝒄𝒐𝒏𝒔𝒕 👇 var x = 10; // function-scoped let y = 20; // block-scoped const z = 30; // block-scoped & cannot be reassigned Key differences that matter in real projects: > "var" is function-scoped and can lead to unexpected bugs due to hoisting. > "let" provides block scope, making control flow safer. > "const" enforces immutability of the reference, improving readability. Modern JavaScript favors const by default, uses let when reassignment is required and avoids var in most cases. Back to fundamentals — with a modern JS lens. Any insights or suggestions are welcome🙂 #DAY6 #JavaScript #WebDevelopment #FrontendDevelopment #LearningInPublic #dailyChallenge
To view or add a comment, sign in
-
𝐯𝐚𝐫 𝐯𝐬. 𝐥𝐞𝐭 𝐯𝐬. 𝐜𝐨𝐧𝐬𝐭: The JavaScript Evolution 🧬 The way we declare variables has changed, and if you are still using `var` in 2026, it might be time for an upgrade! Here is the quick breakdown of the three keywords: 1️⃣𝐯𝐚𝐫 (𝐓𝐡𝐞 𝐎𝐥𝐝 𝐆𝐮𝐚𝐫𝐝) 👴 • 𝐒𝐜𝐨𝐩𝐞: Function Scoped. It ignores block boundaries like `if` or `for` loops, which can lead to variables leaking where they shouldn't. • 𝐇𝐨𝐢𝐬𝐭𝐢𝐧𝐠: It moves to the top and is initialized as `undefined`. This lets you access it before declaration (but it will be undefined). • 𝐁𝐞𝐬𝐭 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐞: Avoid it in modern development. 2️⃣𝐥𝐞𝐭 (𝐓𝐡𝐞 𝐌𝐨𝐝𝐞𝐫𝐧 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞) 🆕 • 𝐒𝐜𝐨𝐩𝐞: Block Scoped. It stays safely inside the `{ curly braces }`. • 𝐇𝐨𝐢𝐬𝐭𝐢𝐧𝐠: It is hoisted, but stays in the 𝐓𝐞𝐦𝐩𝐨𝐫𝐚𝐥 𝐃𝐞𝐚𝐝 𝐙𝐨𝐧𝐞 (𝐓𝐃𝐙). You cannot touch it before the line where it is defined. • 𝐔𝐬𝐞 𝐂𝐚𝐬𝐞: Use this for values that 𝑤𝑖𝑙𝑙 change (like loop counters). 3️⃣𝐜𝐨𝐧𝐬𝐭 (𝐓𝐡𝐞 𝐒𝐭𝐫𝐢𝐜𝐭 𝐎𝐧𝐞) 🔒 • 𝐒𝐜𝐨𝐩𝐞: Block Scoped (just like `let`). • 𝐑𝐮𝐥𝐞: You 𝐦𝐮𝐬𝐭 initialize it immediately, and you 𝐜𝐚𝐧𝐧𝐨𝐭 reassign it. • 𝐓𝐡𝐞 𝐆𝐨𝐭𝐜𝐡𝐚: It makes the 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 immutable, not the 𝑣𝑎𝑙𝑢𝑒. If you make an object `const`, you can still change its properties! 🤯 𝐏𝐫𝐨 𝐓𝐢𝐩: Default to `const`. If you realize you need to change the value later, only then switch it to `let`. Check out the comparison table below! 👇 Do you have a strict linter rule to ban `var` in your projects? #JavaScript #WebDevelopment #ES6 #CodingBestPractices #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
🚀 JavaScript Magic: Why "Undefined" is actually a Feature, not a Bug! I just had a "Wow" moment diving into the JavaScript Execution Context, and it changed how I look at my code. Ever wondered why you can console.log a variable before you even declare it, and JavaScript doesn't lose its mind? 🤯 🧠 The Secret: Two-Phase Execution When your code runs, JavaScript doesn't just start at line 1. It takes two passes: 1.Memory Creation Phase: JS scans your code and allocates space for all variables and functions. 2. Execution Phase: It runs the code line-by-line. ⚡ The var Behavior (Hoisting) If you use var, JavaScript initializes it as undefined during the memory phase. Result: You can log it early. No error, just a quiet undefined. It’s like the variable is there, but its "suit" hasn't arrived yet. 🛑 The let & const Twist (TDZ) Try the same thing with let or const, and the engine throws a ReferenceError. Why? The Temporal Dead Zone (TDZ). While let and const are also "hoisted," they aren't initialized. They stay in a "dead zone" from the start of the block until the moment the code actually hits the declaration. The Lesson: JavaScript isn't just reading your code; it's preparing for it. Understanding the Execution Context makes debugging feel like having X-ray vision. 🦸♂️ Have you ever been bitten by the Temporal Dead Zone, or do you still find yourself reaching for var out of habit? Let’s discuss! 👇 #JavaScript #WebDevelopment #CodingTips #Frontend #Programming101
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
https://github.com/nishchaya2k