🤯 JavaScript Currying: Why does this function keep returning functions? Ever seen code like this and thought “Who hurt you?” 👇 add(1)(2)(3) Why not just write add(1, 2, 3) like a normal human? 😅 Welcome to Currying in JavaScript 👇 🧠 What is Currying? Currying is a functional programming technique where a function that takes multiple arguments is broken into a chain of functions, each taking one argument at a time. const add = a => b => c => a + b + c; add(1)(2)(3); // 6 Each function remembers the previous value using closures. Yes, JavaScript never forgets… 🤔 Why should you care? Because currying helps you write: ✅ Reusable code ✅ Cleaner & readable logic ✅ Partial application (fix some arguments now, pass the rest later const multiply = a => b => a * b; const double = multiply(2); double(5); // 10 const triple = multiply(3); triple(5); // 15 Write once, reuse everywhere 🔥 ⚛️ Where do we actually use this? If you’re a React / Redux developer, you’re already using currying 👀 ⚠️ Currying ≠ Partial Application Quick reminder: ⚛️ Currying → f(a)(b)(c) ⚛️ Partial application → pre-fill some arguments and reuse the function Different tools, same productivity boost 🚀 Middleware, event handlers, utility functions… it’s everywhere. #ReactJS #JavaScript #WebDevelopment #Frontend #MERN #ReactHooks #CleanCode #JavaScript #WebDevelopment #FrontendMagic #CodeWithFun #TechExplainedSimply #mernstack #mern #react #js #JavaScript #WebDevelopment #CodingHumor #FrontendDevelopment #TechEducation #ProgrammingFun #LearnToCode #CodeNewbie #DeveloperCommunity
JavaScript Currying: Cleaner Code with Closures
More Relevant Posts
-
🚀 Stop Guessing How JavaScript Works: The Event Loop Explained Ever wondered why JavaScript is "single-threaded" but can still handle thousands of concurrent tasks without breaking a sweat? The secret isn't magic; it's the Event Loop. 🎡 If you want to master asynchronous programming, you have to understand how these four pieces play together: 1. The Call Stack 📚 This is where the engine tracks what function is currently running. It’s a LIFO (Last In, First Out) structure. If the stack is busy, nothing else happens. 2. Web APIs 🌐 When you call a setTimeout, a fetch request, or a DOM event, JavaScript "hands off" these tasks to the browser (or Node.js). This keeps the main thread free. 3. The Callback Queue (Task Queue) 📥 Once a Web API finishes its job, the callback (the code you want to run) moves here to wait for its turn. 4. The Event Loop 🔄 The "Gatekeeper." It has one simple job: Look at the Call Stack. If the Stack is empty, take the first task from the Queue and push it onto the Stack. 💡 Why does this matter? Have you ever seen a UI freeze during a heavy calculation? That’s because the Call Stack is clogged, and the Event Loop can't push the "render" or "click" tasks from the queue. Pro Tip: Always remember that Microtasks (like Promises) have a "VIP pass." They get executed before the standard Macrotasks (like setTimeout), even if the timer has already expired! #JavaScript #WebDevelopment #ProgrammingTips #Frontend #SoftwareEngineering #EventLoop
To view or add a comment, sign in
-
Back to Basics: The 8 Building Blocks of JavaScript. 🧱 Sometimes we get so caught up in frameworks like React or Angular that we forget the fundamental DNA of the language. JavaScript data types are divided into two main categories: 𝐏𝐫𝐢𝐦𝐢𝐭𝐢𝐯𝐞𝐬 and 𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞𝐬. Knowing the difference is key to avoiding weird bugs (like accidental mutation). 1️⃣𝐓𝐡𝐞 𝟕 𝐏𝐫𝐢𝐦𝐢𝐭𝐢𝐯𝐞𝐬 (𝐈𝐦𝐦𝐮𝐭𝐚𝐛𝐥𝐞 & 𝐏𝐚𝐬𝐬-𝐛𝐲-𝐕𝐚𝐥𝐮𝐞) These are simple values. They don't have methods attached to them (until JS temporarily wraps them). • 𝐍𝐮𝐦𝐛𝐞𝐫: Integers & Floats. • 𝐒𝐭𝐫𝐢𝐧𝐠: Text. • 𝐁𝐨𝐨𝐥𝐞𝐚𝐧: True/False. • 𝐍𝐮𝐥𝐥: Intentionally empty. (The "I know this is empty" value). • 𝐔𝐧𝐝𝐞𝐟𝐢𝐧𝐞𝐝: Unintentionally empty. (The "I haven't set this yet" value). • 𝐒𝐲𝐦𝐛𝐨𝐥: Unique identifiers. • 𝐁𝐢𝐠𝐈𝐧𝐭: For numbers bigger than `2^53 - 1` (added in ES2020). 2️⃣𝐓𝐡𝐞 𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐓𝐲𝐩𝐞𝐬 (𝐌𝐮𝐭𝐚𝐛𝐥𝐞 & 𝐏𝐚𝐬𝐬-𝐛𝐲-𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞) • 𝐎𝐛𝐣𝐞𝐜𝐭: The parent of them all. • 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧: Yes, functions are objects! They are "callable" objects. • 𝐀𝐫𝐫𝐚𝐲𝐬: Special objects with numeric keys. 💡 𝐂𝐥𝐚𝐬𝐬𝐢𝐜 𝐉𝐒 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧: Why does `typeof null` return `'object'`? 𝐴𝑛𝑠𝑤𝑒𝑟: It’s actually a bug from the very first version of JavaScript! It can't be fixed now without breaking the web, so we live with it. 😅 Check out the full list in the infographic below! 👇 How often do you actually use `Symbol` or `BigInt` in your day-to-day work? #JavaScript #WebDevelopment #CodingBasics #SoftwareEngineering #Frontend #JSFundamentals
To view or add a comment, sign in
-
-
The only JavaScript function that comes with a "Pause" button. ⏯️ In JavaScript, the golden rule of functions is usually "Run-to-Completion." Once a function starts, it doesn't stop until it returns or finishes. 𝐄𝐧𝐭𝐞𝐫 𝐭𝐡𝐞 𝐆𝐞𝐧𝐞𝐫𝐚𝐭𝐨𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧. 🤯 It completely breaks the rule. It is a special type of function that can be paused in the middle of execution and resumed later from exactly where it left off. 𝐓𝐡𝐞 𝐌𝐚𝐠𝐢𝐜 𝐒𝐲𝐧𝐭𝐚𝐱: 1️⃣ `function*`: The asterisk tells JS this isn't a normal function. 2️⃣ `yield`: This keyword acts like a pause button. It spits out a value and freezes the function's state. 3️⃣ `.next()`: This is the play button. Call it to resume the function until it hits the next `yield`. 𝐖𝐡𝐲 𝐢𝐬 𝐭𝐡𝐢𝐬 𝐮𝐬𝐞𝐟𝐮𝐥? (𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧) Generators allow for 𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧. You don't have to calculate a list of 1 million items at once (crashing your memory). You can generate them one by one, only when you actually need them. It’s perfect for infinite streams, ID generators, or defining complex state machines. Check out the syntax breakdown below! 👇 Have you used Generators in production (maybe with Redux Saga)? #JavaScript #WebDevelopment #CodingPatterns #AdvancedJS #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
🧠 Most JavaScript devs argue over this — and that’s the point 👀 (Even seniors don’t agree immediately) No frameworks. No libraries. Just how JavaScript actually schedules work. 🧩 Output-Based Question (Event Loop: microtasks vs macrotasks) console.log("A"); setTimeout(() => { console.log("B"); }, 0); Promise.resolve().then(() => { console.log("C"); }); queueMicrotask(() => { console.log("D"); }); console.log("E"); ❓ What will be printed — in the correct order? ❌ Don’t run the code 🧠 Think like the JavaScript engine A. A → E → C → D → B B. A → C → D → E → B C. A → E → D → C → B D. A → E → C → B → D 👇 Drop ONE option only (no explanations yet 😄) Why this matters Most developers know: Promises run before setTimeout But many don’t know: queueMicrotask runs before .then Console order ≠ execution intuition One wrong assumption = flaky UI or race bugs When fundamentals aren’t clear: async bugs feel random production issues are hard to reproduce debugging becomes guesswork Strong JavaScript developers don’t memorize outputs. They understand why the engine schedules work this way. 💡 I’ll pin the full breakdown after a few answers. #JavaScript #EventLoop #AsyncJavaScript #JSFundamentals #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevCommunity #VibeCode
To view or add a comment, sign in
-
-
Understanding Set in JavaScript Recently, I revisited Set in JavaScript, and it’s one of those small features that can greatly improve performance and code clarity. - What is a Set? A Set is a special JavaScript object that stores unique values only — duplicates are automatically removed. const mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(2); // duplicate ignored mySet.add(3); console.log(mySet); // Set(3) {1, 2, 3} - Ways to create a Set From scratch → new Set() From an array → perfect for removing duplicates const arr = [1, 2, 2, 3, 4, 4]; const uniqueValues = new Set(arr); - Useful Set Methods add() → Add value has() → Check if value exists (returns true/false) delete() → Remove value clear() → Remove all values size → Get total count const fruits = new Set(["apple", "banana", "mango"]); fruits.has("banana"); // true fruits.delete("banana"); console.log(fruits.size); // 2 - Why use Set? Stores unique values Easily removes duplicates Faster lookup performance Cleaner logic compared to arrays - Performance matters: Set.has() → O(1) Array.includes() → O(n) Small concepts like this can make a big difference when handling large datasets in real-world applications. - To know more, please visit w3schools.com and MDN 😊 #JavaScript #WebDevelopment #FrontendDevelopment #NodeJS #ReactJS #Programming #SoftwareDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
🧠 99% of JavaScript devs get this event loop question wrong 👀 (Even seniors pause before answering) No frameworks. No libraries. Just how JavaScript actually schedules work. 🧩 Output-Based Question (Event Loop: sync vs microtasks vs macrotasks) console.log("start"); setTimeout(() => { console.log("timeout"); }, 0); Promise.resolve().then(() => { console.log("promise"); }); (async function () { console.log("async"); })(); console.log("end"); ❓ What will be printed — in the correct order? ❌ Don’t run the code 🧠 Think like the JavaScript engine A. start → async → end → promise → timeout B. start → end → async → promise → timeout C. start → async → promise → end → timeout D. start → promise → async → end → timeout 👇 Drop your answer in the comments (no cheating 😄) Why this question matters This tests whether you truly understand: • synchronous execution • the event loop • microtasks vs macrotasks • why Promise.then beats setTimeout(0) • async IIFEs vs promises Many developers “use” async code every day — but few understand when it actually runs. Good JavaScript developers don’t memorize outputs. They understand how the engine thinks. 💡 I’ll pin the full explanation after a few answers. #JavaScript #EventLoop #AsyncJavaScript #WebDevelopment #ProgrammingFundamentals #InterviewPrep #MCQ #DeveloperTips #CodeQuality
To view or add a comment, sign in
-
-
Most developers think closures are some kind of JavaScript “magic”… But the real truth is simpler—and more dangerous. Because if you don’t understand closures: Your counters break Your loops behave strangely Your async code gives weird results And you won’t even know why. Closures are behind: React hooks Event handlers Private variables And many interview questions In Part 7 of the JavaScript Confusion Series, I break closures down into a simple mental model you won’t forget. No jargon. No textbook definitions. Just clear logic and visuals. 👉 Read it here: https://lnkd.in/g4MMy83u 💬 Comment “CLOSURE” and I’ll send you the next part. 🔖 Save this for interviews. 🔁 Share with a developer who still finds closures confusing. #javascript #webdevelopment #frontend #programming #reactjs #learnjavascript #softwareengineering #coding #devcommunity
To view or add a comment, sign in
-
𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗠𝘂𝘀𝘁 𝗞𝗻𝗼𝘄 JavaScript isn’t about memorizing syntax. It’s about understanding how things actually work under the hood. These core concepts decide whether you write working code or production-ready code. 1. Execution Context & Call Stack JavaScript runs code inside execution contexts and manages them using the call stack. This explains why functions execute in order and how stack overflows happen. 2. Hoisting Variables and functions are moved to the top of their scope during compilation. var is hoisted with undefined, while let and const live in the Temporal Dead Zone. 3. Scope & Closures Closures allow functions to remember variables from their parent scope even after execution. This powers data hiding, currying, and many React hooks patterns. 4. this Keyword This is not lexical in JavaScript. Its value depends on how a function is called, not where it’s written. 5. Event Loop & Async JavaScript Promises, async/await, and callbacks, all rely on the event loop, microtask queue, and call stack to handle non-blocking operations. 6. Prototypes & Inheritance JavaScript uses prototype-based inheritance, not classical inheritance. Understanding this clears confusion around classes and __proto__. 7. Shallow vs Deep Copy Objects are copied by reference. Knowing when to deep copy prevents hidden bugs and state mutation issues. 8. Debounce & Throttle Used to control performance in scroll, resize, and input events, critical for real-world apps. Final Thought If you understand these concepts deeply, frameworks become easy. If you skip them, frameworks feel like magic until production breaks. #JavaScript #WebDevelopment #Frontend #JSConcepts #Programming #ReactJS #InterviewPrep #CleanCode
To view or add a comment, sign in
-
JavaScript imports: Old vs Modern — what actually changed? If you started learning JavaScript a few years ago, you probably used require(). Today, most projects use import. At first glance, it looks like just a syntax change. In reality, the difference is much deeper. Here’s what changed. 1. Syntax and structure Old (CommonJS): const fs = require('fs'); module.exports = function greet() { console.log("Hello"); }; Modern (ES Modules): import fs from 'fs'; export function greet() { console.log("Hello"); } 2. When modules are loaded - CommonJS loads modules at runtime. - That means require() is executed when the code runs. - ES Modules are statically analyzed before execution. - The structure of imports and exports is known in advance. - This enables better tooling and optimization. 3. Synchronous vs asynchronous behavior CommonJS is synchronous by default. This works well in Node.js but doesn’t fit browsers perfectly. ES Modules are designed with asynchronous loading in mind and work natively in browsers. 4. Dynamic imports With CommonJS, you can require conditionally: if (condition) { const lib = require('./lib'); } With modern JavaScript, you use dynamic import: if (condition) { const lib = await import('./lib.js'); } 5. Performance and tree-shaking Because ES Modules are statically analyzable, bundlers can remove unused code (tree-shaking). With CommonJS, this is much harder. Why this matters ES Modules are now the standard for modern JavaScript — in browsers and in Node.js. They improve performance, tooling, and maintainability. CommonJS isn’t “wrong” — it’s just older. But for new projects, ES Modules are the better long-term choice. #JavaScript #ESModules #CommonJS #NodeJS #WebDevelopment #FrontendDevelopment #BackendDevelopment #SoftwareEngineering #Programming #Coding #WebDev #JSDeveloper #Tech #CleanCode
To view or add a comment, sign in
-
-
🚀 5 Tricky JavaScript Output Questions (Closures & Scope) Let’s test your JS fundamentals 👇 Try to guess the output before checking answers. 1️⃣ for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } 2️⃣ let a = 10; (function () { console.log(a); let a = 20; })(); 3️⃣ function foo() { console.log(x); var x = 10; } foo(); 4️⃣ const obj = { a: 10, getA() { return this.a; } }; const fn = obj.getA; console.log(fn()); 5️⃣ console.log(typeof typeof 1); Answers below 👇 1️⃣ 3 3 3 (var is function-scoped, same reference) 2️⃣ ReferenceError (Temporal Dead Zone for `let`) 3️⃣ undefined (var hoisting without initialization) 4️⃣ undefined (`this` is lost when function is detached) 5️⃣ "string" (typeof 1 → "number", typeof "number" → "string") If you can explain *why*, you’re interview-ready 💯 #javascript #frontend #interviewprep #webdevelopment
To view or add a comment, sign in
More from this author
Explore related topics
- Front-end Development with React
- Writing Functions That Are Easy To Read
- Ways to Improve Coding Logic for Free
- Coding Best Practices to Reduce Developer Mistakes
- How to Add Code Cleanup to Development Workflow
- Best Practices for Logic Placement in ASP.NET Core Pipeline
- How Developers Use Composition in Programming
- How to Boost Web App Performance
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