💻 JavaScript Intermediate – Flatten a Nested Array Nested arrays can be tricky, but flattening them makes your data easier to work with. 📌 Problem: Convert a nested array into a single-level array. JavaScript code function flattenArray(arr) { return arr.flat(Infinity); } console.log(flattenArray([1, [2, [3, 4]], 5])); 📤 Output: [1, 2, 3, 4, 5] 📖 Explanation: • flat() method converts nested arrays into a single array. • Using Infinity ensures all levels of nesting are flattened. 💡 Tip: Great for working with complex JSON or API responses. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips
Flatten Nested Arrays with JavaScript's flat() Method
More Relevant Posts
-
💻 JavaScript Intermediate – Remove Duplicates from an Array Duplicates in arrays can cause bugs or unnecessary processing. Here's a clean way to remove them. 📌 Problem: Get an array containing only unique values. JavaScript code function removeDuplicates(arr) { return [...new Set(arr)]; } console.log(removeDuplicates([1, 2, 2, 3])); 📤 Output: [1, 2, 3] 📖 Explanation: • Set automatically stores unique values only. • Spread operator ... converts it back to an array. 💡 Tip: Use this for data cleaning, API responses, or user inputs. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips
To view or add a comment, sign in
-
-
🚀 Day 933 of #1000DaysOfCode ✨ Sorting Arrays in JavaScript — Different Types Explained Sorting isn’t always just about numbers in ascending order. In today’s post, I’ve covered sorting arrays in JavaScript for different data types — whether it’s numbers, strings, objects, or more complex structures. The goal is to help you understand how sorting behaves internally and how to customize it properly. If you’ve ever faced unexpected results while using `.sort()`, this post will help you gain clarity and control. 👇 What type of array do you find most challenging to sort? #Day933 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #Next #CodingCommunity
To view or add a comment, sign in
-
📊 Day 14 – Poll Answer & Explanation console.log([1,2] + [3,4]); ❓ What will be the output? Many developers expect array concatenation, but JavaScript behaves differently ✅ Step-by-Step Explanation Step 1️⃣ JavaScript sees the `+` operator. The `+` operator can perform: Addition String concatenation Step 2️⃣ When arrays are used with `+`, JavaScript converts them to strings. [1,2].toString() // "1,2" [3,4].toString() // "3,4" Step 3️⃣ Now the operation becomes: "1,2" + "3,4" Step 4️⃣ This performs string concatenation. "1,23,4" ### 🎯 Final Output 1,23,4 📌 Key Concept The `+` operator with arrays converts them into strings first, then performs string concatenation. #JavaScript #WebDevelopment #FrontendDevelopment #Programming #CodingInterview #DeveloperCommunity #100DaysOfCode #LearnToCode
To view or add a comment, sign in
-
map, filter, reduce ✅ Transform arrays effortlessly with map ✅ Pick only what you need with filter ✅ Summarize data like a champ with reduce Who even needs loops when these exist? Which array function do you swear by? #JavaScript #WebDev #ProgrammingLife #CodeDaily #FrontendDev #SoftwareEngineer #TechLife #CodeNewbie
To view or add a comment, sign in
-
🤯 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭: 𝐖𝐡𝐞𝐫𝐞 𝐋𝐨𝐠𝐢𝐜 𝐆𝐨𝐞𝐬 𝐭𝐨 𝐃𝐢𝐞 (𝐚𝐧𝐝 𝐖𝐡𝐲) If you’ve ever looked at your console and thought, "That shouldn't be possible," you’re not alone. JavaScript is a language built on "best intentions" that often lead to total logic meltdowns. I’ve put together the attached guide (PDF) breaking down of the most infamous "WTFJS" moments—from why 0.1 + 0.2 isn't 0.3 to how the console can literally yell "banana" at you. 🔍 𝐖𝐡𝐚𝐭’𝐬 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐡𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠 𝐮𝐧𝐝𝐞𝐫 𝐭𝐡𝐞 𝐡𝐨𝐨𝐝? Most of these "bugs" actually follow three very specific (and very weird) rules: • 𝐓𝐲𝐩𝐞 𝐂𝐨𝐞𝐫𝐜𝐢𝐨𝐧 (𝐓𝐡𝐞 𝐒𝐢𝐥𝐞𝐧𝐭 𝐊𝐢𝐥𝐥𝐞𝐫): JavaScript hates throwing errors. If you try to subtract a number from a string, it won't stop you—it will just "guess" what you meant. Sometimes it guesses wrong. • 𝐓𝐡𝐞 𝐈𝐄𝐄𝐄 𝟕𝟓𝟒 𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝: JS doesn't store numbers the way humans write them. It uses binary floating-point math. When you deal with large integers or small decimals, you’re seeing the "rounding errors" of the machine. • 𝐓𝐫𝐮𝐭𝐡𝐲 𝐯𝐬. 𝐅𝐚𝐥𝐬𝐲: In JS, an empty array [] is technically "truthy," but when compared to a boolean using ==, the engine performs a series of hidden conversions that defy common sense. 🛡️ 𝐇𝐨𝐰 𝐭𝐨 𝐬𝐭𝐚𝐲 𝐬𝐚𝐧𝐞: • 𝐒𝐭𝐫𝐢𝐜𝐭 𝐄𝐪𝐮𝐚𝐥𝐢𝐭𝐲 (===): Never trust the double equals. • 𝐌𝐚𝐧𝐮𝐚𝐥 𝐂𝐚𝐬𝐭𝐢𝐧𝐠: Don't let JS guess your types; convert them yourself using Number() or String(). • 𝐔𝐬𝐞 𝐁𝐢𝐠𝐈𝐧𝐭: For those massive numbers that keep rounding up. Check out the PDF below for the full breakdown of these logic-defying snippets! Which of these tripped you up the most when you were starting out? Let’s swap horror stories in the comments. 👇 #Javascript #WebDevelopment #Programming #SoftwareEngineering #CodingLife #TechHumor
To view or add a comment, sign in
-
🚀 Day 67 | JavaScript Loops & Array Iteration Today I practiced JavaScript loops and working with arrays of objects 💻 🔹 What I Worked On: • Iterated through array of objects using for loop • Printed all elements and accessed object properties like loc • Used loop with step increment (i += 2) to print alternate values • Practiced reverse counting using for and while loops • Used forEach() for cleaner array iteration 💡 Key Learning: • Arrays of objects are very common in real-world applications • Loop conditions must be handled carefully (i < length vs <= length) • forEach() is simple and readable for iteration • Multiple ways to loop → choose based on requirement 🔥 Takeaway: 👉 Mastering loops is key to handling data efficiently in JavaScript Consistency is improving logic step by step 🚀 #Day67 #JavaScript #Loops #ArrayIteration #ProblemSolving #CodingJourney #10000Coders #WebDevelopment #SravanKumarSir
To view or add a comment, sign in
-
💻 JavaScript Intermediate – Custom map() Function The map() method is widely used to transform arrays. Here’s how you can implement it manually. 📌 Problem: Apply a function to each element of an array and return a new array. function customMap(arr, callback) { let result = []; for (let i = 0; i < arr.length; i++) { result.push(callback(arr[i])); } return result; } let numbers = [1, 2, 3]; let doubled = customMap(numbers, function(num) { return num * 2; }); console.log(doubled); 📤 Output: [2, 4, 6] 📖 Explanation: • map() creates a new array by applying a function to each element. • Here, we manually implemented the same logic using a loop and callback. 💡 Tip: Understanding this helps you grasp how higher-order functions work in JavaScript. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips
To view or add a comment, sign in
-
-
Why JavaScript feels a bit tricky sometimes, The case of "5" - 1 vs. "5" + 1 JavaScript type coercion: console.log("5" - 1); // 4 ✅ Wait... what? console.log("5" + 1); // "51" 🤯 🤔 Why does subtraction work, but addition behaves differently? "-" converts values to numbers → "5" becomes 5 → result is 4 "+" prefers string concatenation → "5" + "1" becomes "51" 🔥 Bonus examples: console.log("5" * 2); // 10 ✅ console.log("5" / 2); // 2.5 ✅ console.log("5" - "2"); // 3 ✅ console.log("5" + "2"); // "52" ❌ 😄 JavaScript sometimes feels like: “Numbers? Strings? Depends on the situation.” these small concepts really show how important fundamentals are. #JavaScript #WebDevelopment #LearningInPublic #Developers #CodingLife #Students
To view or add a comment, sign in
-
🚨 JavaScript Hoisting – Something Most Developers Still Misunderstand Most developers say: 👉 “JavaScript moves variables to the top of the scope.” But that’s not actually what happens. Let’s test this 👇 console.log(a); var a = 10; Output: undefined Now try this: console.log(b); let b = 20; Output: ReferenceError: Cannot access 'b' before initialization 💡 Why the difference? Both var and let are hoisted. But the real difference is initialization timing. ✔ var is hoisted and initialized with undefined during the creation phase. ✔ let and const are hoisted but stay inside the Temporal Dead Zone (TDZ) until the line where they are declared. That’s why accessing them before declaration throws an error. 👉 So technically: JavaScript doesn’t “move variables to the top”. Instead, the JavaScript engine allocates memory for declarations during the creation phase of the execution context. Small detail. But it explains a lot of confusing bugs. 🔥 Understanding this deeply helps when debugging closures, scope issues, and async code. #javascript #frontend #webdevelopment #reactjs #coding #softwareengineering
To view or add a comment, sign in
-
💻 JavaScript Practice: Merging Two Arrays Using a While Loop Today I practiced an important JavaScript concept — merging two arrays using a while loop. It’s a great exercise to improve logical thinking and understand how loops and indexes work together. Instead of using built-in methods like concat() or the spread operator, I tried doing it manually with a while loop. This helps in understanding how data moves step by step inside arrays. Key Idea: Start with two arrays. Use a while loop to iterate through them. Push elements into a new array until all elements are merged. Example: let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6];then let result = [1,2,3,4,5,6] Practicing these small problems helps build a stronger foundation in JavaScript logic and problem-solving. 🚀 #JavaScript #DSA #WebDevelopment #CodingPractice #FrontendDevelopment 😊
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