📘 Day 69: JavaScript Loops (for & while) 🔹 Loops in JavaScript • Loops are used to repeat a block of code multiple times • Helpful for continuous iteration and data processing • <br> is often used with document.writeln() to print on new lines 🔸 For Loop 💡 Structure: for(start; condition; step) • Executes code while the condition is true • Order in JS: 1️⃣ Initialize → 2️⃣ Check condition → 3️⃣ Print → 4️⃣ Increment Example: for(let i=0;i<10;i++) prints 0–9 🔸 Start, Stop, Step • i=0 → start • i<10 → stop condition • i+=2 or i+=3 → step (skip values) • Useful for controlling iteration speed 🔸 For…of Loop • Used to print values from: ✅ Arrays ✅ Sets ✅ Maps Example: for(let i of array) → prints elements one by one 🔸 For…in Loop • Used mainly for Objects • Prints keys (indexes/properties) To print key + value: object[key] Example output: Name : John 🔸 While Loop • Runs while condition is true • Manual increment is required Example: while(i<=10) 🔸 Step in While Loop • Controlled using i+=2, i+=3, etc. • Helps skip numbers and reduce iterations ✨ Today’s focus was mastering iteration techniques in JavaScript — a core skill for handling arrays, objects, and dynamic data efficiently. #JavaScript #Day69 #WebDevelopment #JSLoops #ForLoop #WhileLoop #CodingJourney #FrontendDevelopment #LearnJavaScript #ProgrammingBasics #DeveloperJourney
Mastering JavaScript Loops: For & While
More Relevant Posts
-
🔁 Understanding Loops in JavaScript — A Quick Guide Loops are fundamental in JavaScript when you need to execute a block of code multiple times. Choosing the right loop can make your code more readable and efficient. Here are the most common types of loops in JavaScript 👇 1️⃣ "for" Loop – Best when you know how many times the loop should run. for (let i = 0; i < 5; i++) { console.log("Iteration:", i); } 2️⃣ "while" Loop – Runs as long as the condition remains true. let i = 0; while (i < 5) { console.log("Count:", i); i++; } 3️⃣ "do...while" Loop – Executes at least once before checking the condition. let i = 0; do { console.log("Value:", i); i++; } while (i < 5); 4️⃣ "for...of" Loop – Perfect for iterating over iterable objects like arrays, strings, or maps. const fruits = ["Apple", "Banana", "Mango"]; for (const fruit of fruits) { console.log(fruit); } 5️⃣ "for...in" Loop – Used to iterate over object keys. const user = { name: "Sujit", role: "Frontend Developer" }; for (let key in user) { console.log(key, user[key]); } 💡 Quick Tip: Use "for...of" for arrays and "for...in" for objects to keep your code clean and readable. Mastering loops helps you handle data structures, API responses, and complex logic more efficiently. #javascript #webdevelopment #frontend #codingtips #programming
To view or add a comment, sign in
-
-
🔁 Understanding Loops in JavaScript — A Quick Guide Loops are fundamental in JavaScript when you need to execute a block of code multiple times. Choosing the right loop can make your code more readable and efficient. Here are the most common types of loops in JavaScript 👇 1️⃣ "for" Loop – Best when you know how many times the loop should run. for (let i = 0; i < 5; i++) { console.log("Iteration:", i); } 2️⃣ "while" Loop – Runs as long as the condition remains true. let i = 0; while (i < 5) { console.log("Count:", i); i++; } 3️⃣ "do...while" Loop – Executes at least once before checking the condition. let i = 0; do { console.log("Value:", i); i++; } while (i < 5); 4️⃣ "for...of" Loop – Perfect for iterating over iterable objects like arrays, strings, or maps. const fruits = ["Apple", "Banana", "Mango"]; for (const fruit of fruits) { console.log(fruit); } 5️⃣ "for...in" Loop – Used to iterate over object keys. const user = { name: "Sujit", role: "Frontend Developer" }; for (let key in user) { console.log(key, user[key]); } 💡 Quick Tip: Use "for...of" for arrays and "for...in" for objects to keep your code clean and readable. Mastering loops helps you handle data structures, API responses, and complex logic more efficiently. #javascript #webdevelopment #frontend #codingtips #programming
To view or add a comment, sign in
-
📌 Understanding flat() in JavaScript When working with arrays in JavaScript, especially nested arrays, things can quickly get complex. That’s where the flat() method becomes incredibly useful. 💠 What is flat()? 🔹 The flat() method creates a new array with sub-array elements concatenated into it recursively up to a specified depth. 👉 In simple terms: It “flattens” nested arrays into a single-level array. ⚡ Important Characteristics ✅ Returns a new array (does not modify original) ✅ Removes empty slots in sparse arrays ❌ Works only on arrays (not objects) ❌ Does not deeply flatten objects inside arrays 🎯 Real-World Use Cases ✔ Handling API responses with nested arrays ✔ Processing grouped data ✔ Cleaning complex data structures ✔ Preparing data for mapping or filtering 🚀 Why It Matters Before flat(), developers often used: 🔹 reduce() 🔹 Recursion 🔹 concat.apply() Now, flattening arrays is clean, readable, and expressive. 💡 Final Thought Clean data structures lead to clean logic. Understanding methods like flat() helps you write more maintainable and predictable JavaScript code. #JavaScript #WebDevelopment #FrontendDevelopment #CleanCode #LearningInPublic
To view or add a comment, sign in
-
-
Objects in JavaScript are like containers that store related data together. Think of them as a digital filing cabinet where everything about one thing lives in one place. 🎯 What is an Object? An object is a collection of key-value pairs. Instead of having 10 separate variables, you group them logically. Without objects: let userName = "Sarah" let userAge = 28 let userCity = "NYC" With objects: let user = { name: "Sarah", age: 28, city: "NYC" } See how much cleaner that is? 💡 Why Use Objects? → Organization: Keep related data together → Scalability: Easy to add new properties → Readability: Code makes more sense → Reusability: Pass one object instead of multiple variables 🔧 Essential Object Methods You Need to Know: Object.keys() - Get all property names let user = {name: "John", age: 30} Object.keys(user) // ["name", "age"] Object.values() - Get all values Object.values(user) // ["John", 30] Object.entries() - Get key-value pairs Object.entries(user) // [["name", "John"], ["age", 30]] Object.assign() - Copy or merge objects let newUser = Object.assign({}, user, {city: "LA"}) Object.freeze() - Make object immutable Object.freeze(user) // Can't modify anymore Object.seal() - Allow edits but no new properties Object.seal(user) // Can change values, can't add new keys hasOwnProperty() - Check if property exists user.hasOwnProperty("name") // true 🎁 Pro Tip: Use objects whenever you have data that belongs together. If you're passing more than 2-3 parameters to a function, consider using an object instead. Objects are the foundation of JavaScript. Master them and everything else becomes easier. What's your favorite object method? Drop it in the comments! #JavaScript #WebDevelopment #Coding #Programming #WebDev #LearnToCode #SoftwareDevelopment #CodeNewbie #100DaysOfCode
To view or add a comment, sign in
-
⭕ Mastering JavaScript Array Methods with Practical Examples Understanding array methods like map(), filter(), reduce(), and find() is essential for writing clean and efficient JavaScript code. 🔹 1. map() – Transform Data const prices = [100, 200, 300]; const updatedPrices = prices.map(price => price + 50); console.log(updatedPrices); // [150, 250, 350] Used to transform each element and return a new array. ➖ ➖➖➖➖➖➖➖➖➖➖➖➖➖ 🔹 2. filter() – Select Data const numbers = [1, 2, 3, 4, 5, 6]; const evenNumbers = numbers.filter(num => num % 2 === 0); console.log(evenNumbers); // [2, 4, 6] Used to return elements that match a condition. ➖ ➖➖➖➖➖➖➖➖➖➖➖➖➖ 🔹 3. reduce() – Accumulate Data const cart = [500, 1000, 1500]; const totalAmount = cart.reduce((total, item) => total + item, 0); console.log(totalAmount); // 3000 Used for totals, sums, and complex calculations. ➖➖➖➖➖➖➖➖➖➖➖➖➖ 🔹 4. find() – Find First Match const users = [ { id: 1, name: "Rahul" }, { id: 2, name: "Aman" } ]; const user = users.find(userObj => userObj.id === 2); console.log(user); // { id: 2, name: "Aman" } Used to retrieve a specific item based on a condition. ✨ Strong fundamentals in JavaScript improve React components, backend logic in Node.js, and overall project performance. Continuous learning + practical implementation = Better development skills. #JavaScript #MERN #WebDevelopment #FrontendDevelopment #NodeJS #Coding #Developers
To view or add a comment, sign in
-
Headline: Stop guessing between var, let, and const. 🛑 When I first started with JavaScript, I thought variables were just "boxes" to store data. Then I hit my first "ReferenceError" and realized there’s a lot more going on under the hood—specifically Scope and Data Types. If you're still confused about why we stopped using var or what "Temporal Dead Zone" means, I wrote a comprehensive guide for you. What’s inside: ✅ Why we need variables in the first place. ✅ A breakdown of var vs. let vs. const. ✅ Understanding the 5 basic primitive data types. ✅ Scope explained (in plain English!). Mastering these fundamentals is the difference between writing code that "just works" and writing code that is predictable and clean. Read the full guide here: 👉 https://lnkd.in/g2Bcw6Ag #JavaScript #WebDevelopment #CodingBeginner #ProgrammingTips #TechBlog #Frontend #LearningToCodeMastering these fundamentals is the difference between writing code that "just works" and writing code that is predictable and clean. Read the full guide here: 👉 https://lnkd.in/g2Bcw6Ag #chaicode #cohort2026 #JavaScript #WebDevelopment #CodingBeginner #ProgrammingTips #TechBlog
To view or add a comment, sign in
-
🚀 **JavaScript: var vs let vs const (Every Developer Should Know This)** Understanding the difference between `var`, `let`, and `const` is one of the most important fundamentals in JavaScript. Let’s simplify it 👇 --- 🔴 **var** • Function scoped • Can be **reassigned** • Can be **redeclared** • Hoisted and initialized as `undefined` ```javascript var x = 10; x = 20; // ✅ allowed var x = 30; // ✅ allowed ``` ⚠️ Old JavaScript way. Avoid using `var` in modern code. --- 🟢 **let** • Block scoped `{ }` • Can be **reassigned** • ❌ Cannot be redeclared in the same scope • Hoisted but in **Temporal Dead Zone (TDZ)** ```javascript let y = 10; y = 20; // ✅ allowed let y = 30; // ❌ Error ``` ✔ Best for variables that will change. --- 🟣 **const** • Block scoped • ❌ Cannot be reassigned • ❌ Cannot be redeclared • Hoisted with **Temporal Dead Zone** ```javascript const z = 10; z = 20; // ❌ Error ``` ✔ Best for constants. --- 🎯 **Best Practice** ✔ Use **const by default** ✔ Use **let when value changes** ❌ Avoid **var** This makes your code **cleaner, safer, and predictable.** --- 💬 Interview Question: What is **Temporal Dead Zone (TDZ)** in JavaScript? Comment your answer 👇 --- #JavaScript #WebDevelopment #FrontendDevelopment #Coding #Programming #SoftwareEngineering #LearnToCode #DeveloperTips #JS #TechCommunity
To view or add a comment, sign in
-
-
But I can still change const even though it is immutable, Const a = [] a.push(7) Above code is completely valid. Can anyone explain that .. comment below 👇
Immediate Joiner - Software Developer | Full Stack Web Developer | NODE JS | REACT JS | PHP | JS | GITHUB | PYTHON | DJANGO | REST API | MYSQL | MONGO DB | FLASK | WORDPRESS
🚀 **JavaScript: var vs let vs const (Every Developer Should Know This)** Understanding the difference between `var`, `let`, and `const` is one of the most important fundamentals in JavaScript. Let’s simplify it 👇 --- 🔴 **var** • Function scoped • Can be **reassigned** • Can be **redeclared** • Hoisted and initialized as `undefined` ```javascript var x = 10; x = 20; // ✅ allowed var x = 30; // ✅ allowed ``` ⚠️ Old JavaScript way. Avoid using `var` in modern code. --- 🟢 **let** • Block scoped `{ }` • Can be **reassigned** • ❌ Cannot be redeclared in the same scope • Hoisted but in **Temporal Dead Zone (TDZ)** ```javascript let y = 10; y = 20; // ✅ allowed let y = 30; // ❌ Error ``` ✔ Best for variables that will change. --- 🟣 **const** • Block scoped • ❌ Cannot be reassigned • ❌ Cannot be redeclared • Hoisted with **Temporal Dead Zone** ```javascript const z = 10; z = 20; // ❌ Error ``` ✔ Best for constants. --- 🎯 **Best Practice** ✔ Use **const by default** ✔ Use **let when value changes** ❌ Avoid **var** This makes your code **cleaner, safer, and predictable.** --- 💬 Interview Question: What is **Temporal Dead Zone (TDZ)** in JavaScript? Comment your answer 👇 --- #JavaScript #WebDevelopment #FrontendDevelopment #Coding #Programming #SoftwareEngineering #LearnToCode #DeveloperTips #JS #TechCommunity
To view or add a comment, sign in
-
-
"Var and functions move to the top of your file" ...but do they? Really? Think about it. Open any JavaScript file right now. Add a console.log before a var declaration. Run it. You get undefined — not an error. Call a function 50 lines before you've even written it. It works. Perfectly. Now try the same thing with let or const. It crashes. ReferenceError. If hoisting truly "moves code to the top" — why doesn't it move let and const too? Because nothing is moving anywhere. That explanation you've been hearing since day one? It's a lie. A convenient, oversimplified lie that every tutorial keeps repeating. The truth is — JavaScript reads your code twice. Yes. Twice. The first time, it doesn't execute anything. It just scans your entire file and quietly builds a memory map. Every var it finds gets stored as undefined. Every function gets stored completely. Every let and const gets stored too — but locked. Untouchable. Frozen until their exact line is reached. The second time, it actually runs your code. Line by line. And because memory was already set up, accessing a var before its declaration doesn't crash — it simply finds undefined sitting there, waiting. Nothing moved to the top. The engine just remembered it before you asked. This process has a name that most JavaScript developers have never properly learned — the Execution Context and its two phases. And once you understand it, JavaScript stops being weird. Closures make sense. Scope chains make sense. Even the this keyword starts clicking. I broke this down with code examples and step-by-step visuals on Medium. Link in comments 👇 #JavaScript #WebDevelopment #Programming #SoftwareEngineering #NodeJS #BackendDevelopment
To view or add a comment, sign in
-
-
𝗧𝗶𝗽𝘀 𝗙𝗼𝗿 𝗖𝗹𝗲𝗮𝗻 𝗝𝗮𝗩𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗼𝗱𝗲 You want your JavaScript code to be clean and easy to read. Object destructuring helps you do this. It lets you extract object properties into variables in one line. Here's how it works: - You can extract properties into variables with the same names. - You can set default values if a property does not exist. - You can rename variables if needed. - You can extract nested values easily. - You can use destructuring inside function parameters for cleaner code. For example: ``` is not allowed, using plain text instead const user = { name: "Alex", age: 25 }; const { name, age } = user; console.log(name, age); This will output: Alex 25 You can also set default values: const user = { name: "Alex" }; const { name, age = 18 } = user; console.log(name, age); This will output: Alex 18 Destructuring makes your code shorter and easier to maintain. It is widely used in React, Node.js, and API handling. So, try using object destructuring in your code today. Source: https://lnkd.in/gYEvMh5H
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