Today I tripped over a small JavaScript detail that turned into a solid reminder about how explicit control flow really is. I wrote a 'for' loop intending to move the index by 2 on each iteration: for(let i = 0; i < 8; i + 2) Looked fine at a glance but the loop never progressed. The reason was simple (and humbling): 'i + 2' is just an expression. It does not mutate state. The loop counter never changed, so I ended up with an infinite loop. The correct versions were: i = i + 2 //explicit assignment i += 2 // idiomatic What this reinforced for me: 1. JavaScript will not "do what you mean", it will only what you explicitly tell it 2. Expressions are not equal to assignments 3. Loop mechanics matter more than the logic inside the loop Small mistake, but a good reminder that clarity beats assumption every time. #JavaScript #LeetCode #LearningInPublic #Debugging #Programming
JavaScript Loop Control Flow Mistakes
More Relevant Posts
-
One JavaScript concept that finally clicked for me was closures. Not the textbook definition. The practical part. A function remembers the variables around it, even after that outer function is done running. So state can live quietly where you don’t see it, but it’s still there. At first this felt like magic. Then it felt dangerous. Now it feels useful. Most bugs I’ve had with closures weren’t because JS was weird. They were because I forgot what my code was holding onto. #JavaScript #Coding #Programming
To view or add a comment, sign in
-
15 minutes wasted… and it wasn’t even JavaScript’s fault. I was convinced something was wrong with map(). My array looked fine. But this kept printing in my console: [undefined, undefined, undefined] Here’s what I wrote: const doubled = numbers.map((num) => { num * 2; }); Turns out… I forgot one word: 𝗿𝗲𝘁𝘂𝗿𝗻 When you use { } in map(), you need an explicit 𝗿𝗲𝘁𝘂𝗿𝗻. That tiny detail cost me time. Now I always check one thing: Am I returning something or just running code? A small mistake, easy to miss, yet still humbling. What’s a JS bug that made you question your sanity? 👇 #JavaScript #FrontendDevelopment #WebDevelopment #CodingTips #JuniorDevelopers #LearnToCode #Programming #CodeDebugging #DeveloperLife #CleanCode
To view or add a comment, sign in
-
-
You're writing 10x more JavaScript code than you need to. I just caught myself writing 20 lines of code that should've been 2. Why? Because I forgot these array methods existed. Here's what changed everything for me: The methods that actually matter: ~ .map() transforms every element (goodbye for loops) ~ .filter() finds exactly what you need ~ .reduce() turns arrays into ANY data structure ~ .find() stops the moment it succeeds ~ .some() & .every() for clean conditionals Learning these didn't just shrink my code. It made it: • 10x more readable • Actually debuggable • Way easier to maintain Your teammates will actually understand what you wrote. That's the real win. #JavaScript #WebDevelopment #Programming #CodingTips #SoftwareEngineering
To view or add a comment, sign in
-
JavaScript isn’t confusing. 𝗦𝗸𝗶𝗽𝗽𝗶𝗻𝗴 𝘁𝗵𝗲 𝗳𝘂𝗻𝗱𝗮𝗺𝗲𝗻𝘁𝗮𝗹𝘀 𝗶𝘀. 😉 Today I learned something interesting about 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝘁𝘆𝗽𝗲 𝗰𝗼𝗲𝗿𝗰𝗶𝗼𝗻, and it finally made this make sense 👇 2 + 2 → 4 "2" + "2" → "22" 2 + 2 - 2 → 2 "2" + "2" - "2" → 20 At first glance, the last line looks wrong. But JavaScript is actually being very logical. Here’s what I learned 👇 🔹 + 𝗯𝗲𝗵𝗮𝘃𝗲𝘀 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁𝗹𝘆 If 𝗮𝗻𝘆 𝗼𝗽𝗲𝗿𝗮𝗻𝗱 𝗶𝘀 𝗮 𝘀𝘁𝗿𝗶𝗻𝗴, + performs 𝘀𝘁𝗿𝗶𝗻𝗴 𝗰𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗶𝗼𝗻, not math. "2" + "2" → "22" 🔹 - 𝗱𝗼𝗲𝘀 𝗡𝗢𝗧 𝘄𝗼𝗿𝗸 𝘄𝗶𝘁𝗵 𝘀𝘁𝗿𝗶𝗻𝗴𝘀 When JavaScript sees -, it 𝗳𝗼𝗿𝗰𝗲𝘀 𝗻𝘂𝗺𝗲𝗿𝗶𝗰 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻. "22" - "2" → 22 - 2 → 20 💡 This behavior is called 𝘁𝘆𝗽𝗲 𝗰𝗼𝗲𝗿𝗰𝗶𝗼𝗻. Once the basics are clear, JavaScript stops being “weird” and starts making sense 🚀 #JavaScript #WebDevelopment #LearningInPublic #Programming #Coding #Fundamentals
To view or add a comment, sign in
-
-
JavaScript doesn’t execute code randomly. Every time it runs, it creates an Execution Context ⚡ Understanding this concept makes hoisting and closures much easier to master. 🔹 Two Main Phases: 1️⃣ Memory Creation Phase • Variables are stored as undefined • Functions are stored completely in memory 2️⃣ Code Execution Phase • Code runs line by line • Values get assigned 🔹 Example: var a = 10; function greet() { console.log("Hello"); } 👉 Memory Phase: a → undefined greet → full function 👉 Execution Phase: a → 10 Once you understand Execution Context, debugging becomes much easier and JavaScript starts making logical sense instead of feeling “magical”. Connect with me on LinkedIn: https://lnkd.in/dx7fPEsy #JavaScript #ExecutionContext #FrontendDeveloper #WebDevelopment #Programming #Coding #Developers #TechContent #LearningInPublic 🚀
To view or add a comment, sign in
-
-
Stack vs Heap in JavaScript Primitives (number, string, boolean, etc.) are stored in the stack — copied by value. Objects, arrays, and functions are stored in the heap — copied by reference. Change a primitive → only that variable changes. Change an object → every reference sees the update. Understanding this prevents subtle bugs and improves your JS fundamentals. #JavaScript #WebDevelopment #FullStack #Programming
To view or add a comment, sign in
-
-
🚀 60 Days JavaScript Challenge | Day 4 Today’s practice was about understanding loops and how repetition works in programming. ✅ Problem: Print numbers from 1 to 10 using JavaScript. 💡 Explanation: Instead of writing multiple console.log() statements manually, a for loop helps repeat the same action automatically. A loop has three parts: 1️⃣ Initialization – starting value 2️⃣ Condition – how long the loop should run 3️⃣ Increment – how the value changes each step The loop continues executing until the condition becomes false. 🎯 What I learned today: Loops are powerful because they reduce repetitive code and make programs efficient. Understanding loop flow is essential before moving to advanced problem solving. Consistency continues. Day 5 coming next ✅ #60DaysOfCode #JavaScript #CodingPractice #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
-
Topic: DOM Manipulation in JavaScript | Dynamic Shape Generator In this video, you will learn: 1). How to select HTML elements using JavaScript 2). How to handle button click events 3). How to create and style shapes dynamically 4). How to display elements based on user input Outcome: Understand how JavaScript can dynamically update webpage content using DOM manipulation. Inspired by concepts from GeeksforGeeks. Vikas Kumar #JavaScript #WebDevelopment #Coding #DOM #FrontendDevelopment #LearnToCode #100DaysOfCode #Developers #Programming #GeeksforGeeks
To view or add a comment, sign in
-
🚫 Extra spaces can break your JavaScript code! Ever faced unexpected bugs because of hidden spaces in user input? The trim() method in JavaScript is a simple but powerful solution to clean strings by removing unwanted spaces from both ends. 💡 Perfect for: ✔ Form validation ✔ User input handling ✔ Real-world projects Small concepts. Big impact. Consistency > Complexity 🚀 #JavaScript #WebDevelopment #FrontendDevelopment #LearnJavaScript #CodingTips #JavaScriptBasics #Developers #Programming
To view or add a comment, sign in
-
Mastering JavaScript: Working with Arrays of Objects Using Reduce Just uploaded a comprehensive multi-page PDF guide on how to effectively handle arrays of objects in JavaScript using the reduce method! 🚀 Whether you're summing values, grouping data by properties, counting occurrences, or merging nested arrays, this guide breaks down these essential patterns with clear examples and practical problems. If you want to write cleaner and more efficient code when working with complex data structures, this is for you! Feel free to download the PDF, try out the examples, and share your questions or insights in the comments. Let’s level up our JavaScript skills together! 💻✨ #JavaScript #CodingTips #WebDevelopment #Programming #CodeNewbie #Developer #LearnToCode #TechGuide #FrontEnd #ReduceMethod
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