JavaScript Loop Control Flow Mistakes

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

To view or add a comment, sign in

Explore content categories