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
Var in For Loops: Understanding Async Code Behavior
More Relevant Posts
-
🚨 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
-
Javascript concept: Data Types A small mistake cost me hours debugging. let a = 10; let b = a; b = 20; console.log(a); // 10 But then: let obj1 = { name: "JS" }; let obj2 = obj1; obj2.name = "React"; console.log(obj1.name); // React 💡 Lesson: Primitives → copy by value Objects → copy by reference #JavaScript #WebDevelopment #Frontend #Coding
To view or add a comment, sign in
-
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
To view or add a comment, sign in
-
-
Ever spent hours chasing bugs only to find the simplest mistake? You write code. It doesn’t work. You spend hours debugging. Then suddenly… the bug reveals itself — in the least expected way. Sometimes, the breakthrough is just one insight away. Keep going. #FullStackDeveloper #MERNStack #NodeJS #ReactJS #JavaScript #RESTAPI #ReduxToolkit #TailwindCSS #WebDevelopment #FrontendDevelopment #BackendDevelopment #ResponsiveDesign #TechSolutions #UserFocusedDesign #SoftwareEngineering #CodingLife #DeveloperCommunity #ModernWebApps #ProgrammingTips #WebDevProjects
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
-
-
☕ Synchronous vs Asynchronous JavaScript JavaScript is single-threaded — one task at a time. 🧱 Synchronous → Step-by-step (blocking) ⚡ Asynchronous → Non-blocking (runs in background) 👉 Without async, your UI would freeze during API calls, timers, or heavy tasks. 💡 Thanks to the Event Loop + Task Queue, JS stays fast and responsive. 🚀 Learn async → write better, smoother apps. #JavaScript #Async #WebDevelopment #Coding #Developers #Frontend #Tech #SoftwareEngineering #LearnToCode #chaicode #chaiaurcode Chai Aur Code
To view or add a comment, sign in
-
-
React is killing complex logics day by day more better code optimization better performance new React pattern (use() + async support): #ReactJS #FrontendDevelopment #WebPerformance #LearningInPublic #JavaScript #Consistency #sourcescodedev
To view or add a comment, sign in
-
-
React is killing complex logics day by day more better code optimization better performance new React pattern (use() + async support): #ReactJS #FrontendDevelopment #WebPerformance #LearningInPublic #JavaScript #Consistency #sourcescodedev
To view or add a comment, sign in
-
-
React is killing complex logics day by day more better code optimization and better performance New React pattern (use() + async support): #ReactJS #FrontendDevelopment #WebPerformance #LearningInPublic #JavaScript #Consistency
To view or add a comment, sign in
-
-
5 React habits that will make your teammates actually enjoy reading your code. 🧹 1️⃣ Name your components like they're job titles Bad: `Comp1` Good: `UserProfileCard` 2️⃣ Keep components under 100 lines If it's scrolling forever, it's doing too much. 3️⃣ Custom hooks are your best friend Logic in the JSX = pain. Extract it. 4️⃣ Avoid prop drilling early If you're passing props 3+ levels deep, rethink your state management. 5️⃣ Default exports for components, named exports for utilities Consistency saves debugging time. Clean code isn't a personality trait. It's a habit. #ReactJS #Frontend #JavaScript #CleanCode #WebDevelopment
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