Closures look confusing at first, but the core idea is simple: A closure is when a function remembers variables from the scope where it was created, even after that outer function has finished running. That is why closures are so useful for private state, counters, factories, and callbacks. The infographic breaks it down visually: create a variable, return an inner function, and that inner function keeps access to the remembered value. If you understand this, a lot of JavaScript starts making more sense. What JavaScript concept should I simplify next? #JavaScript #Closures #WebDevelopment #FrontendDevelopment #Programming #LearnToCode #ReactJS #SoftwareEngineering
Understanding JavaScript Closures
More Relevant Posts
-
💡 JavaScript Challenge function sum(a, b) { return a + b; } function sumCurried(a) { return function(b) { return a + b; }; } console.log(sum(5, 4)); console.log(sumCurried(5)(4)); ❓ What’s the output? Two different approaches… one result. But here’s the real question 👇 Are you more curious about the answer… or how both paths lead there? 👇 Drop your answer below! 🔗 Follow me for more insights on coding, creativity & branding. #JavaScript #WebDevelopment #CodingChallenge #Curiosity #Programming #Developers #LearnToCode
To view or add a comment, sign in
-
-
this in JavaScript looks simple… until it starts pointing to the wrong thing 😵 Same function, different outputs — just because of how it’s called. So what does this actually mean? 👉 It’s not about where the function is written 👉 It’s about who is calling the function In this blog, I’ve explained: What this really represents this in global context this inside objects and functions How calling context changes everything Kept it simple with clear examples (no unnecessary theory) ✨ 🔗 Read here: https://lnkd.in/dMkPffeA Would appreciate your feedback 🙌 #javascript #webdevelopment #programming #coding
To view or add a comment, sign in
-
-
Ever wondered why JavaScript shows “undefined” even before a variable is assigned? 🤯 console.log(a); var a = 10; At first glance, this feels confusing… But the answer lies in one powerful concept: 👉 Execution Context Here’s what actually happens behind the scenes: ⚡ When JavaScript runs your code, it creates an Execution Context ⚡ In the memory phase, variables are hoisted → initialized as undefined ⚡ In the execution phase, code runs line by line and values get assigned I made a short video explaining the basics—would love your feedback 🙌 #javascript #webdevelopment #frontend #programming #coding #developers #learntocode #100daysofcode
To view or add a comment, sign in
-
Why does this print 4 4 4 4 in JavaScript? 🤯 A small setTimeout() + var question that teaches a big concept: closures, scope, and async behavior. When you understand why it happens, JavaScript starts making much more sense. #JavaScript #WebDevelopment #Programming #Coding #FrontendDevelopment #SoftwareEngineering #Developer #TechCommunity #LearnToCode #AsyncJavaScript #Closures #SetTimeout #100DaysOfCode #CodingTips #JavaScriptDeveloper
To view or add a comment, sign in
-
-
What is the difference between let, var, and const? `var`, `let`, and `const` are used to declare variables in JavaScript, but they behave differently. `var` is the older way and has function scope, which can lead to unexpected behavior. It also allows redeclaration and can be updated freely. `let` is block-scoped, meaning it only exists within the block where it’s defined. It can be updated but not redeclared in the same scope, making it safer than `var`. `const` is also block-scoped but cannot be reassigned after it’s declared. It’s used for values that should not change. Overall, `let` and `const` are preferred in modern JavaScript. #webdeveloper #tech #coding #programming
To view or add a comment, sign in
-
-
30 Days JavaScript Challenge : Day 24 ✅ Today’s problem was about sorting an array using a custom function. Instead of directly sorting values, we use a function fn to decide the order — basically telling JavaScript how to compare elements. At first it feels simple, but it actually shows how powerful sorting can be when you control the logic: Sorting objects based on a property Sorting nested arrays Custom ranking based on conditions It’s one of those concepts that looks basic but is used everywhere in real projects. Slowly getting more clarity on how to write flexible and reusable logic in JavaScript. #javascript #leetcode #webdevelopment #frontenddeveloper #codingchallenge #learninginpublic #developers #programming #buildinpublic
To view or add a comment, sign in
-
-
LeetCode Day 20 : Problem 15 (3Sum) Just solved my LeetCode problem for today. It was "3Sum", find all unique triplets in an array that add up to zero. Sounds like a natural extension of Two Sum, right? Just add a third pointer. But here's what I actually learned: The naive extension doesn't work cleanly. Three nested loops give you O(n³) and you still have to deduplicate the output, which becomes its own problem. The real insight is that sorting first unlocks everything else. Once the array is sorted, you fix one number with an outer loop and reduce the remaining problem to exactly Two Sum II on the rest of the array. Two pointers, moving inward, same logic as yesterday. O(n²) total, one pass per fixed element. But the deduplication is where this problem actually lives. There are three places duplicates can sneak in and each needs its own guard. The first is the outer pointer. If nums[i] equals nums[i-1], you've already explored every triplet that starts with this value. Skip it. The second and third are the inner pointers after finding a valid triplet. If nums[left] equals nums[left+1], moving left forward once would just find the same triplet again. Same for the right side. So you skip past all consecutive duplicates before moving both pointers inward. The order of those inner skips matters too. You skip duplicates first, then move the pointers. Not the other way around. Sorting is doing most of the heavy lifting here. It makes the two-pointer approach possible, it makes duplicate detection trivial with a single comparison, and it guarantees that once your fixed element is positive, no valid triplet can exist ahead of it since two positives can never sum to zero with another positive. The real lesson? Sorting is often the unlock that turns an ugly O(n³) problem into a clean O(n²) one. Before writing nested loops, ask whether sorting the input first changes what's possible. #DSA #LeetCode #JavaScript #CodingJourney #Programming
To view or add a comment, sign in
-
-
Day 24/30 — JavaScript Journey Error Handling 🚫 Bugs will happen. Crashes are optional. ⚡ Smart devs don’t avoid errors… They control them. ✅ try...catch → handle runtime failures ✅ throw → create meaningful errors ✅ finally → always clean up ✅ async/await + try...catch → no silent failures ✅ Custom Errors → debug like a pro Bad code breaks. Good code survives. Great code recovers. 💡 Handle errors smart. That’s where real engineering begins. 🚀 #JavaScript #WebDevelopment #Coding #Programming #SoftwareEngineering #DevTips
To view or add a comment, sign in
-
-
The new keyword looks simple… but most people use it without knowing what it actually does 👀 That’s exactly where confusion starts. Why does a function suddenly create objects? Where does this point? And how do methods magically appear? 👉 It’s all because of new. In this blog, I’ve explained: What new actually does behind the scenes How constructor functions create objects How objects link to shared methods (prototype) Why everything breaks without new Kept it simple, with step-by-step explanation and examples ✨ 🔗 Read here: https://lnkd.in/dyZwek-x Would love your feedback 🙌 Next: Constructor Functions or this keyword? #javascript #webdevelopment #programming #coding
To view or add a comment, sign in
-
-
🚀 Mastering JavaScript Fundamentals Understanding core concepts like Array & String methods is a game-changer for writing clean, efficient code. From map() and filter() to split() and trim(), these built-in methods help you solve problems faster and write smarter logic. 💡 Consistency in learning the basics builds a strong foundation for advanced development. Sharing this quick visual guide to help beginners and refresh concepts for developers! What’s your most used JavaScript method? 👇 #JavaScript #WebDevelopment #Coding #LearnToCode #Frontend #Programming #Developers
To view or add a comment, sign in
-
More from this author
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