JavaScript Looping Guide: For, For...Of, For...In, While

🚀 𝐓𝐡𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐨𝐨𝐩𝐢𝐧𝐠 𝐆𝐮𝐢𝐝𝐞 𝐟𝐨𝐫: The classic. Best when you know exactly how many times you need to run. 𝐟𝐨𝐫...𝐨𝐟: Modern and clean. Perfect for iterating over values in an array or string. 𝐟𝐨𝐫...𝐢𝐧: Use this for objects. It iterates over the keys (properties). 𝐰𝐡𝐢𝐥𝐞: Best when the number of iterations is unknown and depends on a condition. 𝐟𝐨𝐫𝐄𝐚𝐜𝐡(): The functional approach for arrays. Great for readability, though you can't "break" out of it early. // 1. The Standard For Loop for (let i = 0; i < 5; i++) {  console.log(`Index: ${i}`); } // 2. For...Of (Best for Arrays) const fruits = ['🍎', '🍌', '🍇']; for (const fruit of fruits) {  console.log(fruit); } // 3. For...In (Best for Objects) const user = { name: 'Alex', role: 'Dev' }; for (const key in user) {  console.log(`${key}: ${user[key]}`); } // 4. While Loop (Condition-based) let energy = 3; while (energy > 0) {  console.log("Coding...");  energy--; } 💡 𝐏𝐫𝐨 𝐓𝐢𝐩: If you are dealing with large datasets and need to transform them, consider array methods like .map() or .filter()—they are often more expressive than a standard loop! Feel free to reach me out for any career mentoring Naveen .G.R|CareerByteCode #javascript #webdevelopment #codingtips #programming #frontend

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories