“A ‘for’ loop that forgot to fill its blanks 🧩” I’ve always been fond of empty statements, but this time, I found something even more curious — An empty expression inside a for loop! 🤯 We’re all familiar with loops like this 👇 for (let i = 0; i < 5; i++) { console.log(i); } But did you know JavaScript allows a loop to run even when one of its expressions is completely missing? 😮 For example 👇 let i = 0; for ( ; ; ) { if(i==5){ break; } console.log(i++); } Here’s why this works so beautifully 🔍👇 👉 1️⃣ Flexible structure: The for loop doesn’t require all three expressions — initialization, condition, and increment are optional. 👉 2️⃣ Power of control: You can move parts like initialization or increment outside or inside the loop body — giving you freedom to control logic more precisely. 🧠 👉 3️⃣ Clarity through simplicity: By skipping the increment part, your focus is solely on the condition. This makes the loop’s intent clearer and the code cleaner — a real example of “Less is more.” 💡 So next time you spot a for loop with an empty expression, don’t rush to call it incomplete — it might just be showing you that silence in code can still speak logic! 😎 #JavaScript #ForLoop #CodingTips #WebDevelopment #CleanCode #ProgrammingHumor #DevelopersCommunity #LearnJavaScript #CodeWisdom
"Empty expressions in for loops: A JavaScript trick"
More Relevant Posts
-
After ~2.5 months of inconsistency, I finally sat down to code today. But this time I’m doing it differently: Not “learn a new syntax and move on.” Instead: train logic, build thinking, sharpen tools. Today’s focus: reversing a string — not once, but in multiple styles. Why? Because the same problem solved in different ways builds real depth. But the biggest win wasn’t even the logic — It was discovering how to streamline my workflow: 👉 I set up VS Code so I can run my JS file fresh every time just by pressing Ctrl + S. Now every save = instant execution in the terminal. No clicking, no re-running, no friction. Small setup improvement → massive boost in momentum. #javascript #learninginpublic #webdevelopment #problemsolving #100daysofcode
To view or add a comment, sign in
-
-
Day 9 of #30DaysOfJavaScript: Mastering Function Execution Control with Closures! 🔐 Today I solved an interesting problem that pushed me to create a function wrapper called once. This wrapper ensures the original function runs only one time, no matter how many times it’s called afterwards—any further calls simply return undefined. This challenge deepened my understanding of JavaScript closures and how they can be used to preserve state and control function execution flow. Here’s my implementation: javascript function once(fn) { let called = false; let res; return function(...args) { if (!called) { called = true; res = fn(...args); return res; } return undefined; }; } Why this matters: Enables safer function calls by preventing unintended multiple executions. Useful pattern in scenarios like event handling, API calls, or initialization. Showcases how closures can encapsulate state elegantly in JavaScript. Excited to keep building on these foundational concepts and to see what Day 10 has in store! If you're on a similar learning path, let’s connect to share insights and grow together. #JavaScript #Closures #FunctionalProgramming #LeetCode #CodingChallenge #WebDevelopment #SoftwareEngineering #LearningJourney
To view or add a comment, sign in
-
-
🔥 JS Loops Demystified: for vs for…in vs for…of If you’ve ever wondered which loop to use in JavaScript, you’re not alone. Choosing the wrong one can cause subtle bugs! Here’s a quick guide: 1️⃣ Classic for loop – The old reliable const arr = [10, 20, 30]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } Use when you need index access, or want full control over iteration. 2️⃣ for…in loop – Iterate over keys (property names) const arr = [10, 20, 30]; for (let key in arr) { console.log(key); // 0, 1, 2 } Works on objects too. ⚠️ Can be dangerous for arrays — also loops over inherited properties. 3️⃣ for…of loop – Iterate over values const arr = [10, 20, 30]; for (let value of arr) { console.log(value); // 10, 20, 30 } Perfect for arrays, strings, maps, sets. Safe, clean, and readable. 💡 Pro tip: Use for when you need the index. Use for…of for values (most array iterations). Avoid for…in on arrays — reserve it for objects. Small choice, huge impact on readability and bug prevention. #JavaScript #CodingTips #WebDevelopment #FrontendDev #CleanCode #JSProTips #ForOfVsForIn
To view or add a comment, sign in
-
🧩 A Little Loop Story — for vs forEach in JavaScript While revisiting some old code today, I noticed something curious about loops. Both for and forEach help us iterate through arrays — yet they behave quite differently. Here’s what I observed 👇 🔹 The for Loop 👉 It gives you complete control — you can start anywhere, stop anywhere, and even skip elements. But, there’s a catch…🤔 👉 If the condition isn’t set properly, it might run infinitely! 😅♾️ 👉 Also, variables declared inside a for loop may still exist after it finishes, which can consume extra space. 👉 On the bright side, it’s simple, familiar, and easy to follow. 🔹 The forEach Loop 👉 This one plays by the rules — always runs from start to end. 👉 You can’t break midway, but you also can’t accidentally loop forever. 🙌 And the best part? 👉 Variables used inside its callback vanish after execution, keeping memory usage cleaner. 👉 Though its syntax looks a bit complex, it keeps your code organized and less error-prone. 💭 My takeaway: 👉 If you want control and flexibility, choose for. 👉 If you prefer cleaner and safer looping, go with forEach. #JavaScript #WebDevelopment #FrontendTips #CleanCode #CodingThoughts #TechLearning #CodeSmarter #DeveloperCommunity #ProgrammingBasics #LearnWithMe
To view or add a comment, sign in
-
🚀 Ever wondered why a for loop with var logs 3,3,3 — but the same loop with let logs 0,1,2? It’s not magic — it’s JavaScript’s scoping and binding behavior at play 👇 🧩 The Classic Mystery for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } // Output: 3, 3, 3 for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } // Output: 0, 1, 2 💡 The Hidden Truth var is function-scoped, so it creates one single binding for the entire loop. ➜ All 3 callbacks share the same i. ➜ By the time they run, i = 3. let is block-scoped — and in a for loop, JavaScript creates a fresh binding per iteration. ➜ Each callback remembers its own copy of i. ➜ So it prints 0, 1, 2. ⚙️ Behind the Scenes You can imagine it like this: 🧠 var → One shared box 🧳 🧠 let → A new box every iteration 📦 When the async setTimeout runs later, all var callbacks open the same box (value = 3), while each let callback opens its own box (0, 1, 2). ✅ What You Should Use In modern JavaScript: 🔹 Always prefer let or const over var. They give cleaner scoping, fewer bugs, and predictable async behavior. 💬 Have you ever debugged this “3,3,3” issue before? Drop your story or how you solved it ⬇️ #JavaScript #WebDevelopment #Coding #JSDeepDive #Frontend #Developers #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 26 — JavaScript Foundations: var, let, const & Core Interactions 💻⚡ Today’s session deepened my understanding of JavaScript fundamentals — how data is declared, stored, and interacted with at the most essential level. 💡 Topics Covered: • Difference between var, let, and const — scope, re-declaration, and modern best practices • Hands-on with prompt( ), alert( ), and console.log( ) — understanding how data flows between user and browser • Real-world logic on how browsers interpret and execute scripts • Setting the foundation for DOM interactions and event-driven programming ✨ Each line of code felt like unlocking a new layer of control — from dynamic user input to precise debugging insights. The fundamentals may look simple, but they form the core muscle of every advanced JS concept. This is where true coding confidence begins. 💪 #JavaScript #FrontendDevelopment #FullStackDeveloper #CodingJourney #WebDevelopment #LearnInPublic #BuildInPublic #WebProgramming #SoftwareEngineering #Innovation #TechLearning #JavaScriptFundamentals #ProgrammingBasics #6MonthChallenge
To view or add a comment, sign in
-
-
𝗘𝘃𝗲𝗿 𝘄𝗼𝗻𝗱𝗲𝗿𝗲𝗱 𝘄𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝗯𝗲𝗵𝗶𝗻𝗱 𝘁𝗵𝗲 𝘀𝗰𝗲𝗻𝗲𝘀 𝘄𝗵𝗶𝗹𝗲 𝘆𝗼𝘂𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗰𝗼𝗱𝗲 𝗶𝘀 “𝘄𝗮𝗶𝘁𝗶𝗻𝗴”? 🤔 Spoiler: ̶i̶t̶’s̶ ̶n̶o̶t̶ ̶r̶e̶a̶l̶l̶y̶ ̶w̶a̶i̶t̶i̶n̶g̶.̶ When you run JavaScript, everything starts in a single thread — but it’s far from being “blocking”. Your browser (or Node.js) uses the Event Loop to keep things flowing smoothly. Let’s break it down 👇 🧱 𝗖𝗮𝗹𝗹 𝗦𝘁𝗮𝗰𝗸 Where your functions live and execute — top-down. When a function finishes, it’s popped off the stack. 🌀 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 The “traffic controller” that checks if the stack is empty and then pulls tasks from queues: 𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 𝗤𝘂𝗲𝘂𝗲 (e.g. setTimeout) 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲 (e.g. Promises / await) Here’s a simple example: 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐('1️⃣ 𝚂𝚝𝚊𝚛𝚝'); 𝚜𝚎𝚝𝚃𝚒𝚖𝚎𝚘𝚞𝚝(() => 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐('2️⃣ 𝚃𝚒𝚖𝚎𝚘𝚞𝚝'), 𝟶); 𝙿𝚛𝚘𝚖𝚒𝚜𝚎.𝚛𝚎𝚜𝚘𝚕𝚟𝚎().𝚝𝚑𝚎𝚗(() => 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐('3️⃣ 𝙿𝚛𝚘𝚖𝚒𝚜𝚎')); 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐('4️⃣ 𝙴𝚗𝚍'); 𝗢𝘂𝘁𝗽𝘂𝘁 1️⃣ Start 4️⃣ End 3️⃣ Promise 2️⃣ Timeout Wait... why does the Promise run before the Timeout? 🤔 Because microtasks (Promises) have higher priority than macrotasks (Timeouts). 💭 Takeaway: JavaScript isn’t slow — it’s just predictably asynchronous. Understanding the Event Loop helps you debug timing issues, race conditions, and unexpected logs. Next time your code “pauses”... it’s probably just your Event Loop doing its job while you grab a coffee ☕ Have you ever been surprised by JavaScript’s execution order? Drop your favorite example below 👇
To view or add a comment, sign in
-
-
🤯 𝐓𝐡𝐞 𝐕𝐚𝐫𝐢𝐚𝐛𝐥𝐞 𝐕𝐢𝐛𝐞 𝐂𝐡𝐞𝐜𝐤: 𝒗𝒂𝒓 𝐯𝐬. 𝒍𝒆𝒕 𝐯𝐬. 𝒄𝒐𝒏𝒔𝒕 If you've ever felt like your JavaScript variables were causing trouble, you might be right! Choosing the wrong declaration keyword can lead to bugs, leaks, and general chaos. Here is the quick, no-nonsense guide to who these three are and when to use them: 1. 𝒗𝒂𝒓(𝐓𝐡𝐞 𝐓𝐫𝐨𝐮𝐛𝐥𝐞𝐦𝐚𝐤𝐞𝐫) 🚫 𝐒𝐜𝐨𝐩𝐞: 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧-𝐬𝐜𝐨𝐩𝐞𝐝 (it leaks out of loops and blocks). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed. 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: A legacy keyword. 𝐃𝐨𝐧'𝐭 𝐮𝐬𝐞 𝐢𝐭 in modern code. 2. 𝒍𝒆𝒕 (𝐓𝐡𝐞 𝐅𝐥𝐞𝐱𝐢𝐛𝐥𝐞 𝐎𝐧𝐞) 📝 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝 ({ } are its boundaries). 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: Allowed (you can change the value). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: Use it for variables where the value 𝐧𝐞𝐞𝐝𝐬 𝐭𝐨 𝐜𝐡𝐚𝐧𝐠𝐞 (like a counter in a loop or a score). 3. 𝒄𝒐𝒏𝒔𝒕 (𝐓𝐡𝐞 𝐃𝐞𝐟𝐚𝐮𝐥𝐭 𝐂𝐡𝐨𝐢𝐜𝐞) ✅ 𝐒𝐜𝐨𝐩𝐞: 𝐁𝐥𝐨𝐜𝐤-𝐬𝐜𝐨𝐩𝐞𝐝. 𝐑𝐞-𝐚𝐬𝐬𝐢𝐠𝐧𝐦𝐞𝐧𝐭: 𝐍𝐨𝐭 𝐚𝐥𝐥𝐨𝐰𝐞𝐝 (the binding is permanent, though object properties can still change). 𝐓𝐡𝐞 𝐋𝐞𝐬𝐬𝐨𝐧: This should be your 𝐝𝐞𝐟𝐚𝐮𝐥𝐭 𝐜𝐡𝐨𝐢𝐜𝐞 for almost everything. Use it unless you know the value will be updated. 𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: Start with const. If you get an error because you need to update the variable later, and only then, switch to let. Save yourself the debugging headache! What percentage of the time do you find yourself using const? I'm aiming for 90% plus! #JavaScript #WebDevTips #CodingBestPractices #FrontEndDevelopment #constletvar
To view or add a comment, sign in
-
What is Function and types of Functions in javaScript? I. Function is block of reusable code to Perform specific task. II. It eliminate need of writing same code again and again. III. It allow programmers to divide big program into small and manageable function. Types of Functions :- 1. Default Function (normal Function) 2. Parameterized Function 3. Anonymous Function 4. Function Expression 5. Arrow Function 6. Callback Function
To view or add a comment, sign in
-
Explore related topics
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