Var in for loops with closure formed part 3 for (var i = 0; i < 3; i++) { (function(j) { setTimeout(function() { console.log(j); }, 1000); })(i); } Now this time per iteration of loop with async code it creates closure and IIFE has own scope so it takes different values per iteration value of loop as closure have so it prints 0 ,1 , 2 #webdev #javascript #coding #programming #frontend #backend #fullstack #reactjs #nodejs #html #css #developer #tech #softwareengineering #FrontendDevelopement
JavaScript Closure with IIFE in Loops
More Relevant Posts
-
Var in for loops part 2 Did you Know actual flow of code ? for (var i = 0; i < 3; i++) { setTimeout(function() { console.log(i); }, 1000); } Var in for loop actually prints 0, 1, 2 but because of async code waits in Macrotask with 3 times and when come with for execution it picks latest loop value thats why it prints same value 3 , 3 , 3 #React #ReactJS #ReactDeveloper #FrontendDevelopment #JavaScript #WebDevelopment #FrontendEngineer #UIEngineering #ComponentBased #Hooks #NextJS #SinglePageApplications #WebApps #SoftwareEngineering #CleanCode #DevCommunity #BuildInPublic #Coding #TechCareers #DeveloperLife #Javascript
To view or add a comment, sign in
-
-
🚨 JavaScript Tricky Question #1 What will be the output of this code? 🤔 console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); 💬 Comment your answer 👇 🔁 Follow for daily JS questions #javascript #webdevelopment #frontend #developers #programming #coding #nodejs #reactjs #softwareengineering
To view or add a comment, sign in
-
Why it is recommended that "let" is used in for loops instead of "var" while output is same in both cases ? .......................... Because 1. Scope difference (main reason) var → function scoped let → block scoped With var, the variable exists outside the loop block. 2. Major difference with async code This is where let becomes very important. Using var 3. Prevents accidental bugs With var ✅ Rule used in modern JavaScript Use let for variables that change Use const for variables that don't change Avoid var in modern code #webdevelopment #webdev #webdeveloper #frontend #frontenddeveloper #backend #backenddeveloper #fullstack #fullstackdeveloper #coding #programming #javascript #html #css #reactjs #nodejs #webdesign #softwaredeveloper #tech #devlife
To view or add a comment, sign in
-
-
Two lines. Same numbers. Completely different outputs 🤯 👉 "console.log(1 + 2 + "3");" 👉 "console.log("1" + 2 + 3);" Looks simple… but JavaScript has its own way of thinking 👀 Are you sure you know what the output will be? Watch till the end to understand how JavaScript actually evaluates expressions and why order matters more than you think! #JavaScript #WebDevelopment #Coding #Frontend #Developers #JSBasics
To view or add a comment, sign in
-
-
🚨 JavaScript Tricky Question #2 (Hard) What will be the output? 🤯 for (var i = 0; i < 3; i++) { setTimeout(() => { console.log(i); }, 1000); } 💬 Comment your answer 👇 🔁 Follow for daily JS questions #javascript #webdevelopment #frontend #developers #coding #nodejs #reactjs #interviewprep #programming
To view or add a comment, sign in
-
Two lines. Same numbers. Completely different outputs 🤯 👉 "console.log(1 + 2 + "3");" 👉 "console.log("1" + 2 + 3);" Looks simple… but JavaScript has its own way of thinking 👀 Are you sure you know what the output will be? I made a quick video breaking this down — and trust me, it’s NOT what most people expect 🚀 Watch till the end to understand how JavaScript actually evaluates expressions and why order matters more than you think! #JavaScript #WebDevelopment #Coding #Frontend #Developers #JSBasics
To view or add a comment, sign in
-
🚀 Mastering AbortController in JavaScript/ React (A Must-Know for Modern Developers) ✅ Why should you care? ✔ Prevents memory leaks ✔ Avoids unnecessary API calls ✔ Improves performance It allows you to cancel ongoing async operations like fetch requests. 🔥 React Use Case: useEffect(() => { const controller = new AbortController(); fetch(''https://lnkd.in/d5dTeXqf'', { signal: controller.signal }) .then(res => res.json()) .then(setData) .catch(err => { if (err.name !== 'AbortError') console.error(err); }); return () => controller.abort(); // cleanup }, []); Always clean up API calls in useEffect — especially in React Strict Mode, where effects run twice in development. Follow for more 🚀 Chinmay Kulkarni #javascript #webdevelopment #frontend #reactjs #reactdeveloper #coding #programming #developers #softwareengineering #tech #interviewprep #100daysofcode #learninpublic #reactperformance #asyncjavascript
To view or add a comment, sign in
-
🚀 Default Export vs Named Export in React — Stop Confusing Them! 🔹 Default Export ✔ Only one per file ✔ No curly braces needed ✔ You can rename it while importing export default Header; import MyHeader from "./Header"; --- 🔹 Named Export ✔ Multiple exports allowed ✔ Must use curly braces ✔ Name must match while importing export const add = () => {}; import { add } from "./utils"; --- 🔥 Key Difference 👉 Default = One main thing 👉 Named = Multiple utilities --- Nishant Pal 💬 Which one do you use more — Default or Named? #ReactJS #JavaScript #WebDevelopment #Frontend #Coding #Developers #LearnToCode
To view or add a comment, sign in
-
-
🚀 Node.js vs Express.js — What’s the Differences? 👉 Key Difference: Node.js is the engine 🛠️ Express.js is the toolkit 🧰 that makes development easier #NodeJS #ExpressJS #BackendDevelopment #JavaScript #WebDevelopment #Coding #Developers
To view or add a comment, sign in
-
-
Breaking down core concepts in a simple way helps in understanding React better and faster. • Managing data inside components • Handling side effects like API calls & timers • Understanding lifecycle behavior in modern React #ReactJS #WebDevelopment #Frontend #JavaScript #Coding #Tech
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