Best JavaScript Interview Question: 🚀 The Sneaky Semicolon That Changed My Array! We’ve all been there: staring at a piece of JavaScript code, wondering why the output isn’t what we expected. Sometimes, the culprit is as small as a semicolon. Let’s look at this classic example: const length = 4; const numbers = []; for (var i = 0; i < length; i++) { numbers.push(i + 1); } console.log(numbers); // [1, 2, 3, 4] ✅ Without the semicolon, everything works as expected. The loop runs 4 times, pushing 1, 2, 3, 4 into the array. Now watch what happens when we accidentally add a semicolon after the for loop: const length = 4; const numbers = []; for (var i = 0; i < length; i++); { // <- sneaky semicolon! numbers.push(i + 1); } console.log(numbers); // [5] 😱 Suddenly, instead of [1, 2, 3, 4], we get [5]. Why does this happen? 1. That semicolon ends the loop immediately. 2. The loop runs, incrementing i until it reaches 4. 3. The block { numbers.push(i + 1); } is no longer part of the loop — it executes once after the loop finishes. At that point, i is 4, so i + 1 is 5. Result: [5]. Key Takeaways 1. A stray semicolon can completely change your program’s logic. 2. Always be mindful of where you place semicolons in JavaScript. 3. Tools like ESLint can catch these mistakes before they cause headaches. Prefer let or const over var to avoid scope confusion. 💡 Pro Tip: If you’ve ever debugged for hours only to find a tiny typo or semicolon was the issue, you’re not alone. Share this with your network , it might save someone else from a late‑night debugging session! Follow me for more such learning. #javascript #debuging #webdeveloper #frontenddeveloper #codewithramkumar
This is really amazing that a single semicolon make the complete change of the programing, FE developers must read this
A normal semicolon can change the output of the program in JavaScript. 😆