Today I practiced an important JavaScript concept: Flattening a nested array manually using loops. Instead of using the built-in .flat() method, I implemented the logic myself to deeply understand how flattening actually works. 🧠 Problem Given this array: [1, [2, 3], [4, 5], 6] Return: [1, 2, 3, 4, 5, 6] 🔍 My Approach Created an empty result array. Looped through each element of the main array. Checked if the current element is an array using Array.isArray(). If it’s an array: Loop through it Push each inner element individually into result If it’s not an array: Push it directly into result 💡 Key Line That Does the Magic result.push(arr[i][j]); This line: Accesses elements inside the nested array Pushes them individually into the final result Removes one level of nesting 🎯 What I Learned How nested loops work in real problems The difference between pushing an array vs pushing its elements What “one-level flattening” actually means How .flat() works internally ⏱ Time Complexity O(n) — every element is visited once. Building fundamentals > memorizing shortcuts. Next step: Flatten deeply nested arrays using recursion 🔥 #JavaScript #FrontendDeveloper #ProblemSolving #WebDevelopment #100DaysOfCode #DSA #LearningInPublic
More Relevant Posts
-
🚨 Ever seen JavaScript code that looks like a staircase? 💡 In JavaScript, a callback is a function that runs after a task finishes. For example, after fetching data from an API. Sounds easy… until multiple tasks depend on each other. Then the code starts looking like this: ➡️ Get users ➡️ Then get their posts ➡️ Then get comments ➡️ Then get the comment author Every step waits for the previous one. And suddenly code becomes a deep pyramid of nested functions, often called the “Pyramid of Doom” or "Sideways Triangle." ⚠️ Why developers avoid it: 🔴 Hard to read 🔴 Hard to debug 🔴 Hard to maintain ✨ Modern JavaScript solves this with: ✅ Promises ✅ async / await Both make asynchronous code cleaner and easier to understand. What JavaScript concept confused you the most when you started learning? 👇 Let’s discuss. #JavaScript #WebDevelopment #CodingJourney #AsyncProgramming #LearnInPublic
To view or add a comment, sign in
-
-
📣 𝗡𝗲𝘅𝘁 𝗕𝗹𝗼𝗴 𝗶𝘀 𝗛𝗲𝗿𝗲! ⤵️ Arrow Functions in JavaScript — A Simpler Way to Write Functions ⚡🧠 Regular functions work fine. But when logic becomes small and frequent, typing all that syntax starts feeling heavy. This blog explains arrow functions in a clear, beginner-friendly way — focusing on syntax, mental models, and real usage. 🔗 𝗥𝗲𝗮𝗱 𝗵𝗲𝗿𝗲: https://lnkd.in/gMBAZxX5 𝗧𝗼𝗽𝗶𝗰𝘀 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 ✍🏻: ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⇢ What arrow functions actually are ⇢ Converting normal functions to arrow step by step ⇢ Basic syntax and parameter rules ⇢ Implicit return — the real magic ⇢ When implicit return does NOT work ⇢ Using arrow functions in map(), filter(), and callbacks ⇢ Common beginner mistakes and how to fix them ⇢ When to use arrow functions vs regular functions ⇢ Practical examples for faster understanding 💬 If functions still feel verbose or repetitive, this article helps you write cleaner and more modern JavaScript. #ChaiAurCode #JavaScript #ArrowFunctions #WebDevelopment #ProgrammingBasics #Beginners #LearningInPublic #100DaysOfCoding
To view or add a comment, sign in
-
-
JavaScript Deep Dive [Master Functions part-2] : this, Arrow Functions & More.. One of the most confusing topics in JavaScript is how the this keyword behaves in different situations. Many developers struggle with the difference between regular functions and arrow functions. To make this concept easier, I’ve shared a new PDF: “Mastering JavaScript Context: this & Arrow Functions.” In this guide, you’ll learn: ✅ How this works in JavaScript execution context ✅ The key difference between regular functions and arrow functions ✅ Dynamic runtime binding vs lexical binding ✅ Why arrow functions don’t have their own this ✅ Practical insights into IIFEs and function execution and more. Understanding function context is critical for writing clean, predictable, and bug-free JavaScript, especially when working with objects, event handlers, and modern frameworks. 📄 Check out the PDF and share your thoughts. 🔰 check out First Part of Mastering Functions on my profile. #JavaScript #JavaScriptDeveloper #WebDevelopment #FrontendDevelopment #Programming #Coding #SoftwareDevelopment #LearnJavaScript #JSFunctions #ArrowFunctions #DeveloperCommunity #MERN #learnJavascript #learnReact #js #adityathakor #aditya
To view or add a comment, sign in
-
💡 JavaScript Tip: Start using the .at(-1) today! If you're still accessing the last element of an array like this: arr[arr.length - 1] There’s a cleaner and more readable way 👇 arr.at(-1) 🔹 Why use .at()? ✅ Cleaner syntax ✅ Easier to read ✅ Supports negative indexing 🔹 Examples const nums = [10, 20, 30, 40]; nums.at(0); // 10 nums.at(2); // 30 nums.at(-1); // 40 (last element) nums.at(-2); // 30 🔹 When to use it? Accessing last or second-last elements Writing cleaner, more modern JavaScript Avoiding repetitive .length calculations ⚠️ Note .at() works in modern JavaScript (ES2022+), so ensure your environment supports it. Small improvements like this can make your code more readable and elegant ✨ Are you using .at() already? #JavaScript #CleanCode #WebDevelopment #FrontendDevelopment #ProgrammingTips #DevCommunity #SoftwareEngineering
To view or add a comment, sign in
-
-
Shipping is a habit! 🚀 2 Days, 2 Projects, and a deeper dive into JavaScript. I’ve been heads-down in the #ChaiAurCode journey, and the momentum is real. Over the last 48 hours, I’ve moved from understanding basic logic to shipping functional mini-projects. Project 1: Dynamic List Creator 📝 Focused on mastering the DOM. It’s one thing to see a list on a screen, it’s another to handle real-time user input, state updates, and clean element creation dynamically. 🌐 Try the List Creator: https://lnkd.in/grMrRqMt Project 2: Color Palette Generator 🎨 This one was a technical "Aha!" moment. 🌐 Try the Palette Generator: https://lnkd.in/gCUwyhrc Key takeaways: ➡️ Precision Math: Implementing Math.random() * (max - min) + min to control color tones (Light vs. Dark). ➡️ String Manipulation: Using .padStart(2, "0") to ensure valid Hex codes a small detail that prevents major UI bugs. ➡️ The Backend Loop: I even experimented with running an Express server and frontend logic in a single file to visualize the full Request-Response cycle. Big thanks to my mentors Hitesh Choudhary and Suraj Kumar Jha for the guidance during the T-class sessions! Github repo links - List Creator - https://lnkd.in/gH9hzGY3 Palette Generator - https://lnkd.in/gEAv7NJ4 (I've shared the screen recording for the List Creator in the comments below! 👇) Anirudh J. Akash Kadlag Jay Kadlag Piyush Garg #WebDevelopment #JavaScript #BuildInPublic #FullStackJourney #LearningTogether #Vercel #CodingProgress
To view or add a comment, sign in
-
🚀 JavaScript Spread vs Rest Operator — Same Syntax, Opposite Purpose! Understanding the difference between Spread (...) and Rest (...) operators is essential for writing clean and modern JavaScript code. Although both use the same ... syntax, they perform completely opposite tasks. 🔹 Spread Operator (...) Expands values outward • Breaks an iterable into individual elements • Useful for merging arrays or cloning objects • Common in function calls and object/array literals Example: const a = [1,2,3]; const b = [4,5,6]; const merged = [...a, ...b]; // [1,2,3,4,5,6] 🔹 Rest Operator (...) Collects values into one place • Gathers multiple arguments into an array • Used in function parameters and destructuring • Must always be the last parameter Example: function sum(...nums){ return nums.reduce((a,b) => a + b, 0); } 📌 Key Rule to Remember Spread → Expands values Rest → Collects values Small JavaScript concepts like this make a big difference in writing cleaner and more efficient code. 💬 What other JavaScript concepts should I explain next? If this helped you: 👍 Like | 💬 Comment | 🔁 Repost #JavaScript #WebDevelopment #FrontendDevelopment #SoftwareDevelopment #Programming #Coding #Developer #JavaScriptTips #TechLearning #FullStackDeveloper #DevCommunity #LearnToCode
To view or add a comment, sign in
-
-
Ever copied an object in JavaScript… and later wondered why changing one thing broke something else? I used the spread operator, Object.assign—everything looked right. The object was “copied,” features worked, and I moved on. Until one small change inside a nested object started affecting the original data. That’s when I realized copying in JavaScript isn’t as straightforward as it looks. I used to think: “If I copy an object, I get a completely separate version.” Sounds reasonable—but it misses an important detail. So I slowed down and explored what was actually happening. References. Memory locations. Nested structures. I built tiny examples, tweaked values, compared outputs—sometimes it worked, sometimes it didn’t… until patterns started to show up. And then it made sense. The issue wasn’t the syntax—it was understanding what gets copied and what still points to the same place in memory. That shift changed a lot: • I stopped assuming copies were independent • Debugging weird state changes became much easier • Spread vs structuredClone finally had a clear difference • Nested objects stopped feeling unpredictable Most importantly: 👉 A shallow copy duplicates only the top level 👉 Nested objects still share the same reference 👉 A deep copy creates a fully independent structure Now when something changes unexpectedly, I don’t guess—I check how the data was copied. Still learning, but this one concept made JavaScript feel a lot more predictable. What’s one JS concept that took you time to truly understand? #JavaScript #WebDevelopment #Frontend #LearningInPublic #JSConcepts #Debugging
To view or add a comment, sign in
-
-
If you misunderstand `𝐯𝐚𝐫`, `𝐥𝐞𝐭`, 𝐚𝐧𝐝 `𝐜𝐨𝐧𝐬𝐭`, your bugs will multiply quietly. Master these three, and your JavaScript becomes predictable. I like to explain them with simple mental models. `𝐯𝐚𝐫` is like a basic box. You can put something in it, replace it, and even access it outside the block where it was created. It’s flexible—but that flexibility often creates confusion due to function scope and re-declarations. `𝐥𝐞𝐭` is a box with a protective boundary. You can change what’s inside, but only within its block scope. Step outside that boundary, and it no longer exists. This makes your code safer and more controlled. `𝐜𝐨𝐧𝐬𝐭` is a locked cage. You cannot reassign it to something else. However, if it holds an object or array, the contents can still change—because the reference is locked, not the internal data. Understanding this difference prevents scope leaks, accidental overwrites, and unpredictable behavior. Here’s a practical rule: * Use `𝐜𝐨𝐧𝐬𝐭` by default * Use `𝐥𝐞𝐭` when reassignment is necessary * Avoid `𝐯𝐚𝐫` in modern JavaScript Clean code starts with disciplined variable declarations. When reviewing your code, are you choosing the right “box” intentionally—or out of habit? #JavaScript #FrontendDevelopment #WebEngineering #CleanCode #ProgrammingFundamentals #SoftwareEngineering #CodeQuality
To view or add a comment, sign in
-
-
🚀 JavaScript Fundamentals: Beyond the Basics Ever shipped a bug that was just == vs ===? Or "copied" an object... only to watch the original mutate anyway? These aren't beginner mistakes — they're traps that catch experienced devs too. Mastering JS core mechanics isn't about interviews. It's about writing predictable, production-ready code. Here are the 4 pillars worth revisiting: 🔵 Null vs. Undefined null = intentional absence. You put it there. undefined = JS saying "nothing was ever assigned here." Same result. Very different meaning. ⚖️ == vs. === == tries to be helpful by converting types first. === doesn't. It checks value and type — no surprises. Default to ===. Always. 🔄 Type Coercion JS will silently convert types behind the scenes. The + operator is the sneakiest of all — it both adds and concatenates depending on context. Know the rules before they bite you. 📦 Deep vs. Shallow Copy { ...spread } only copies the top level. Nested objects? Still pointing to the same reference. structuredClone() is your modern, built-in answer to true deep copying. These four concepts will save you hours of debugging and make your logic significantly more robust. Which one tripped you up the most when you were learning? Drop it below 👇 #JavaScript #WebDevelopment #Frontend #CodingTips #SoftwareEngineering #Programming #InterviewPrep
To view or add a comment, sign in
-
-
🧠 Ever wondered how JavaScript keeps track of which function is running? JavaScript uses something called the Call Stack. Think of it like a stack of tasks where functions are added and removed as they execute. 🔹 How the Call Stack Works JavaScript follows a Last In, First Out (LIFO) rule. That means: The last function added to the stack is the first one to finish. Example function first() { second(); } function second() { third(); } function third() { console.log("Hello from third function"); } first(); What happens in the Call Stack 1️⃣ first() is pushed to the stack 2️⃣ second() is called → pushed to the stack 3️⃣ third() is called → pushed to the stack 4️⃣ third() finishes → removed from stack 5️⃣ second() finishes → removed 6️⃣ first() finishes → removed 🔹 Visualising the Stack Call Stack at peak: - third() - second() - first() - Global() Then it unwinds back to the Global Execution Context. 💡 Why This Matters Understanding the call stack helps you understand: - Execution order - Stack overflow errors - Debugging JavaScript - Async behaviour It’s one of the core mechanics of the JavaScript engine. Next post: The Event Loop 🚀 #JavaScript #CallStack #Frontend #WebDevelopment #LearnJS #Programming #LearningInPublic
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