🧠 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 & 𝗟𝗲𝘅𝗶𝗰𝗮𝗹 𝗦𝗰𝗼𝗽𝗶𝗻𝗴 — You Know ? Ever wondered how some functions “remember” variables even after they’ve finished executing? That’s the magic of Closures — one of the most powerful concepts in JavaScript. Here’s how it works 👇 ➡️ 𝗟𝗲𝘅𝗶𝗰𝗮𝗹 𝗦𝗰𝗼𝗽𝗶𝗻𝗴 means a function can access variables defined in its outer scope. ➡️ 𝗖𝗹𝗼𝘀𝘂𝗿𝗲 happens when an inner function remembers and uses variables from its outer function — even after the outer function has returned. #frontend #javascript #coding #learning #developer Muhammad Arham Amna BB Muhammad Awais Raza Awais IftikharAbdul Rehman Asghar Ali Abdul Rehman Waseem
How Closures Work in JavaScript: A Simple Explanation
More Relevant Posts
-
𝗘𝘃𝗲𝗿 𝘄𝗼𝗻𝗱𝗲𝗿𝗲𝗱 𝘄𝗵𝘆 𝗮 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲 𝘄𝗼𝗿𝗸𝘀 𝗶𝗻 𝗼𝗻𝗲 𝗽𝗮𝗿𝘁 𝗼𝗳 𝘆𝗼𝘂𝗿 𝗰𝗼𝗱𝗲 𝗯𝘂𝘁 𝗻𝗼𝘁 𝗶𝗻 𝗮𝗻𝗼𝘁𝗵𝗲𝗿? That’s because of 𝘀𝗰𝗼𝗽𝗲. It decides where your variables live and how long they exist. Think of it like your house. You can use your toothbrush in your bathroom (𝗹𝗼𝗰𝗮𝗹 𝘀𝗰𝗼𝗽𝗲), but not in your neighbour’s (𝗴𝗹𝗼𝗯𝗮𝗹 𝘀𝗰𝗼𝗽𝗲 doesn’t cover that). I revisited this today while working with functions and variables. Seeing how 𝗹𝗼𝗰𝗮𝗹 and 𝗴𝗹𝗼𝗯𝗮𝗹 𝘀𝗰𝗼𝗽𝗲𝘀 interact reminded me how easy it is to overlook small details that break your code. 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Know your 𝘀𝗰𝗼𝗽𝗲𝘀. It’s a small thing that prevents big headaches. 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻: What’s one 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗰𝗼𝗻𝗰𝗲𝗽𝘁 you wish you understood earlier? 👉 Learning by solving. 💻 #KabikaLearnsJS #Javascript #webdevelopment #frontenddev #coding
To view or add a comment, sign in
-
-
#Understanding JavaScript #Promises! Today, I learned about one of the most powerful concepts in JavaScript — Promises 🌟 A Promise is used for handling asynchronous operations. It represents the “eventual” completion (or failure) of a task and helps us avoid callback hell 😅 👉 Basic syntax: let promise = new Promise((resolve, reject) => { // async code here }) ✨ resolve() and reject() are callbacks provided by JavaScript to handle success or failure. Promises make code cleaner, more readable, and easier to manage — especially when combined with .then() and .catch(). #JavaScript #WebDevelopment #Promises #CodingJourney #LearningEveryday #FrontendDevelopment
To view or add a comment, sign in
-
-
💡 Understanding Object Methods in JavaScript Working with objects is fundamental in JS. Here's a quick overview of some powerful methods: Object.keys(obj) → Returns all keys of the object. Object.values(obj) → Returns all values of the object. Object.entries(obj) → Returns key-value pairs as arrays. obj.hasOwnProperty("property") → Checks if the object has a specific property. Object.assign({}, obj, { newProperty: "newValue" }) → Creates a new object by merging existing ones. 👉 View the full example on GitHub: https://lnkd.in/dDN-vDkD #JavaScript #FullStack #100xDevs #WebDevelopment #Coding #JSConcepts #LearnJavaScript
To view or add a comment, sign in
-
Accidentally Discovered Something Cool in JavaScript! 😎 While working on some JavaScript code, I accidentally came across the ! and !! operators - and it turned out to be a really interesting find 😄 At first, I was confused 🤔 but after a bit of testing, it all made sense! - The ! (NOT) operator converts true -> false, and vice versa. - The !! (Double NOT) operator converts any value into its boolean equivalent value and its great for checking if something is truthy or falsy. This tiny trick makes conditions cleaner and helps when validating inputs or checking existence of values in JavaScript. Tiny discoveries like these remind me how fun it is to explore and learn JavaScript every day! 😊 . . . . . . . #WebDevelopment #JavaScript #Frontend #CodingJourney #LearningByDoing #Developer #Coding #LearningToCode #FrontendDevelopment #DevelopersCommunity #CodingTips #CodeWithMe #CleanCode #CodeBetter #DevLife
To view or add a comment, sign in
-
-
Let’s talk about something small but mighty in JavaScript, the Spread Operator (...). You’ve probably seen it before those three dots that seem to be doing “magic.” But what they really do is expand (or “spread out”) the contents of an array or object. In simple terms, the spread operator helps you: - Copy arrays or objects without changing the original one. - Combine multiple arrays or objects easily. - Pass array elements as separate arguments in functions. For example, if you have an array of numbers and you want to copy it, you can use the spread operator instead of manually looping. The same thing applies to merging two arrays or adding new properties to an object. It’s one of those small features that make your code cleaner and easier to understand, and you’ll see it a lot when working with frameworks like React. #JavaScript #LearnInPublic #WebDevelopment #Coding #Backend #100DaysOfCode #Tech
To view or add a comment, sign in
-
-
🚀 Did you know? In JavaScript, setTimeout(..., 0) doesn’t actually run immediately. It runs after all Promises and await calls — even if the delay is 0ms 🧠 Why? Because JS runs all microtasks (like Promises and await) before moving on to macrotasks (like setTimeout, setInterval, etc.). 💡 Takeaway: Understanding the event loop helps you fix async bugs and write smoother, predictable code. #JavaScript #Coding #WebDevelopment #EventLoop #AsyncProgramming #CodeTips #Developers #Learning #letsLearnWithPrateek #Day7 Here’s proof 👇
To view or add a comment, sign in
-
-
Ever had #JavaScript act "too chill" about your mistakes? #TypeScript fixes that. It’s like JavaScript, but with superpowers. It helps you catch errors before they happen, not after. So I’m starting a new series: 🎯 #LearnTypeScriptWithMe! Short, visual lessons + quick quizzes you can finish in a scroll. Topic for Day-1: Why TypeScript exists🤔 Swipe through the carousel and see why so many #devs are switching to TS 💙 By the way, what’s one JS bug that made you say, "I should’ve used TypeScript"? 👇 #LearnTypeScript #Developers #CodingCommunity #LearningCommunity #LearningChallenge #LearnInPublic #WebDevelopment #TypeScript #LearnTogether #UI
To view or add a comment, sign in
-
🚀 JavaScript: Who Executes First? 🤔 If you’ve ever wondered how JavaScript decides what runs first, you’re not alone! 😅 Let’s decode the mystery of Synchronous, Microtask, and Macrotask once and for all 👇 💡 Execution Order in JavaScript: 1️⃣ Synchronous Tasks 🧩 These are executed immediately, line by line. Nothing else happens until all synchronous work is done. 2️⃣ Microtasks ⚡ After the synchronous code completes, Microtasks (like Promises) get their turn. They have higher priority and run before rendering or macrotasks. 3️⃣ Macrotasks ⏱️ Finally, Macrotasks (like `setTimeout` or `setInterval`) execute. They’re queued to run after all microtasks are finished. ✅ In short: 👉 Synchronous → Microtask → Macrotask Understanding this order is a superpower 🧠 — it helps you debug async behavior, improve performance, and write smoother, more predictable JavaScript! 🔥 So next time your console output surprises you… remember who runs first 😉 #JavaScript #WebDevelopment #Frontend #AsyncJS #Programming #Developers #SoftwareEngineering #TechTips #Coding #WebDev #LearnToCode #CodeNewbie #JS #TechEducation #EventLoop #100DaysOfCode #DevCommunity #Engineer
To view or add a comment, sign in
-
🧠 JavaScript Concepts I Explored Today Today was a solid learning session — I spent time understanding how functions actually shape the logic in JavaScript. Here’s what I went through 👇 Functions and how they make code reusable First-class and higher-order functions (functions inside functions 🔁) Closures — the part where JS “remembers” variables even after execution IIFE — functions that run instantly (good for isolated scopes) Arrays — map, filter, reduce, forEach Objects — destructuring, looping, optional chaining These concepts connected so many dots for me. The more I write JavaScript, the more I realize how expressive it can be when you actually understand the “why” behind the syntax. #JavaScript #CodingJourney #WebDevelopment #Frontend #MERNStack
To view or add a comment, sign in
-
Think `undefined` is a keyword? Bless your heart. It's a global property. In non-strict mode, you can literally reassign it: `undefined = true;`. Need a truly untouchable `undefined`? Use the `void` operator: `void 0`. The catch? A teammate will "clean up" your "useless code," reintroducing the very bug you were trying to squash. Is this a genius hack for defensive coding, or proof that JavaScript is built on a house of cards? #GaboTips #JS
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