Day 43/100 – An Important JavaScript Concept Many People Ignore Copying objects and arrays looks simple… Until bugs start appearing. 📌 Topic: Shallow Copy vs Deep Copy ✅ Shallow Copy Creates a new object, but nested objects still reference the original. Examples: Object.assign() Spread operator { ...obj } Problem: Changing nested data affects both copies. ✅ Deep Copy Creates a completely independent copy, including nested objects. Examples: structuredClone(obj) JSON.parse(JSON.stringify(obj)) 👉 Use deep copy when working with complex nested data. Understanding this saves you from painful debugging later. Learning the basics deeply, one day at a time. On to Day 44 🚀 #100DaysOfCode #JavaScript #WebDevelopment #LearningInPublic #Consistency #Frontend
JavaScript Shallow vs Deep Copy: Understanding the Difference
More Relevant Posts
-
🔥 𝐓𝐡𝐞 𝐒𝐞𝐜𝐫𝐞𝐭 𝐋𝐢𝐟𝐞 𝐨𝐟 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 — 𝐖𝐡𝐚𝐭 .𝐜𝐥𝐨𝐧𝐞() 𝐑𝐞𝐚𝐥𝐥𝐲 𝐌𝐞𝐚𝐧𝐬🤯 Most devs know how to copy data in JS. But few realize how each method behaves under the hood. Understanding this can save performance, prevent bugs, and improve clarity. There’s more to cloning than just “duplicate this object.” Here’s what you should know: • 📌 Reference vs Value — Not everything actually copies • 🧠 Shallow clone — Copies top level, not nested objects • 🪄 Deep clone — Copies whole structure • ✨ Spread operator (...) — Short but shallow • 🧱 Object.assign() — Also shallow • 🔁 JSON.parse(JSON.stringify()) — Deepish, but loses functions • 🌪️ StructuredClone — True deep clone with edge-case safety • 🧩 Lodash/utility clone — library tools that avoid common traps ➡️ Shallow clone without knowing deeper references leads to side effects. ➡️ JSON.parse loses types, dates, undefined, functions — beware. ➡️ Modern structured clone is the safest way for true deep copies. Knowing how to clone right improves code clarity and eliminates side effects that hide like ghosts. 👇 What’s your go-to way to clone complex objects in JS? #JavaScript #WebDevelopment #CodingWisdom #DeveloperLife #Frontend #SoftwareEngineering #Programming #CleanCode #Performance #JS2026 #TechTips #DevCommunity
To view or add a comment, sign in
-
-
💡 The Call Stack tracks execution. ❓ What causes a stack overflow error? The call stack is like a to-do list for JavaScript. Every time a function runs, it gets added (pushed) to the stack. When it finishes, it gets removed (popped). 👉 A stack overflow error happens when too many functions are added to the stack and it runs out of space. 🔹 Most Common Cause: Infinite Recursion function loop() { loop(); // calling itself forever } loop(); // ❌ Maximum call stack size exceeded Here, loop() keeps calling itself without stopping. Each call adds a new frame to the stack, and eventually the stack becomes full. 🔹 Proper Recursion (with base case) function countDown(n) { if (n === 0) return; // base case countDown(n - 1); } countDown(5); // ✅ works fine The base case stops the recursion and prevents overflow. ⚠️ Other Causes Deep nested function calls Large recursive algorithms without limits Circular function calls 💡 Takeaway A stack overflow happens when functions keep stacking up without finishing. Always make sure recursive functions have a clear stopping condition. #learnwithisnaan #JavaScriptTips #ModernJavaScript #ES6 #DeveloperTips #CleanCode #JSDevelopers
To view or add a comment, sign in
-
-
👯♂️ "I changed the copy, why did the original update too?!" 😱 If you’ve ever screamed this at your monitor, you’ve fallen into the Shallow Copy Trap. 🪤 In JavaScript, objects and arrays are reference types. When you copy them, it matters how you do it. 1️⃣ The Shallow Copy (The "Surface" Clone) Methods like the spread operator [...] or Object.assign() only copy the first layer of data. - If your object has nested objects inside (like user.address.city), the copy points to the same memory location as the original. - Result: You change the copy's address, and the original user's address changes too. Bugs everywhere. 🐛 2️⃣ The Deep Copy (The "True" Clone) This creates a completely independent duplicate, including all nested levels. - The Old Way: JSON.parse(JSON.stringify(obj)) (Hack, but works for simple data). - The Modern Way: structuredClone(obj) (Native, fast, and handles dates/maps correctly). 🚀 Next time you are updating state in React or manipulating complex data, ask yourself: Do I need a clone, or do I need a twin? What is your go-to method for deep cloning these days? structuredClone or Lodash? 👇 #JavaScript #WebDevelopment #CodingTips #Frontend #ReactJS #SoftwareEngineering #Programming
To view or add a comment, sign in
-
-
Data Structures & String Magic Today was all about how we store and manipulate data in JavaScript. I moved beyond simple variables and explored the power of Strings and Objects. It’s fascinating to see how similar Strings and Arrays can be, yet how unique they are in their behavior! My Learning Milestones Today: String Mastery: Explored slice, join, concat, and the importance of case normalization (toLowerCase/toUpperCase) for comparisons. The "Reverse" Challenge: Learned three different ways to reverse a string—a classic interview favorite! Object Essentials: Introduction to Properties and Values. I practiced multiple ways to "get" and "set" data using dot notation and bracket notation. Advanced Object Ops: Working with nested objects, using Object.keys() and Object.values(), and learning how to safely delete properties. Iteration: Mastering how to loop through objects to pull out the data I need. Objects are truly the backbone of JavaScript, and I'm feeling much more confident in how to structure my code. 🚀 #JavaScript #WebDevelopment #CodingJourney #StringsAndObjects #FrontendDev #TechGrowth
To view or add a comment, sign in
-
-
Javascript arrays are more powerful than you think. 🚀 I used to write complex for loops for data manipulation. Then I truly understood the power of array methods. It’s not just about shorter code—it’s about readability and intent. Three underused methods that deserve more love: .some(): Checks if at least one element passes a test. Great for validation. .every(): Checks if all elements pass. Perfect for form checking. .reduce(): The Swiss Army knife. From summing numbers to reshaping entire data structures. Clean code isn't about being clever; it's about making your logic obvious to the next developer who reads it (which is usually you, six months later). Which array method do you find yourself using the most? #JavaScript #Coding #SoftwareEngineering #WebDev #CleanCode
To view or add a comment, sign in
-
Day 23 – JavaScript 🚀 Shallow Copy vs Deep Copy Understanding how data is copied in JavaScript is important to avoid unexpected bugs, especially when working with objects and arrays. In this post, I’ve covered: • What is a shallow copy • What is a deep copy • Differences between shallow and deep copy • Common methods like spread operator, Object.assign, and JSON methods 📌 Day 23 of my JavaScript learning series. #JavaScript #WebDevelopment #FrontendDevelopment #BackendDevelopment #LearningInPublic #100DaysOfCode #JavaScriptBasics #Developers #CodingJourney
To view or add a comment, sign in
-
-
𝗪𝗲𝗹𝗰𝗼𝗺𝗲 𝘁𝗼 𝗗𝗮𝘆 𝟵 Have you ever updated one variable… and another one changed automatically? 𝙋𝙖𝙪𝙨𝙚 𝙛𝙤𝙧 𝙖 𝙨𝙚𝙘𝙤𝙣𝙙 — 𝙬𝙤𝙪𝙡𝙙 𝙮𝙤𝙪 𝙚𝙭𝙥𝙚𝙘𝙩 𝙩𝙝𝙞𝙨 𝙘𝙤𝙙𝙚 𝙩𝙤 𝙢𝙪𝙩𝙖𝙩𝙚 𝙗𝙤𝙩𝙝 𝙫𝙖𝙡𝙪𝙚𝙨? { 𝚕𝚎𝚝 𝚞𝚜𝚎𝚛𝟷 = { 𝚗𝚊𝚖𝚎: "𝙰𝚕𝚎𝚡" }; 𝚕𝚎𝚝 𝚞𝚜𝚎𝚛𝟸 = 𝚞𝚜𝚎𝚛𝟷; 𝚞𝚜𝚎𝚛𝟸.𝚗𝚊𝚖𝚎 = "𝙹𝚘𝚑𝚗"; 𝚌𝚘𝚗𝚜𝚘𝚕𝚎.𝚕𝚘𝚐(𝚞𝚜𝚎𝚛𝟷.𝚗𝚊𝚖𝚎); // "𝙹𝚘𝚑𝚗" } Why did user1 change? Because: • Both variables point to the same memory address • No copy was created • Only the reference was shared 𝘛𝘩𝘪𝘴 𝘪𝘴 𝘑𝘢𝘷𝘢𝘚𝘤𝘳𝘪𝘱𝘵’𝘴 𝘝𝘢𝘭𝘶𝘦 𝘷𝘴 𝘙𝘦𝘧𝘦𝘳𝘦𝘯𝘤𝘦 𝘣𝘦𝘩𝘢𝘷𝘪𝘰𝘳 — 𝘢𝘯𝘥 𝘪𝘵’𝘴 𝘥𝘦𝘦𝘱𝘭𝘺 𝘵𝘪𝘦𝘥 𝘵𝘰 𝘮𝘦𝘮𝘰𝘳𝘺. 𝙏𝙝𝙚 𝙈𝙚𝙣𝙩𝙖𝙡 𝙈𝙤𝙙𝙚𝙡 (𝙈𝙚𝙢𝙤𝙧𝙮 𝙁𝙞𝙧𝙨𝙩) JavaScript mainly uses two memory areas: -> Stack Memory • Stores primitive values • Fixed size • Fast access -> Heap Memory • Stores objects, arrays, functions • Dynamic size • Accessed via references 𝙒𝙝𝙚𝙣 𝙥𝙖𝙨𝙨𝙚𝙙 𝙗𝙮 𝙫𝙖𝙡𝙪𝙚: A copy of the actual value is passed. Primitives (number, string, boolean, null, undefined, symbol) are always passed by value. 𝙒𝙝𝙚𝙣 𝙥𝙖𝙨𝙨𝙚𝙙 𝙗𝙮 𝙧𝙚𝙛𝙚𝙧𝙚𝙣𝙘𝙚: A memory address (reference) is passed, not the object itself. Objects & arrays live in heap memory. #JavaScript #JSFundamentals #MemoryManagement #FrontendDevelopment #ReactJS #BugFixing #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
Hii Folks 🙂 Yesterday, while discussing the JavaScript Event Loop with a senior, I realized something important. Most of us explain the Event Loop using queues and the call stack. That explanation is correct, but it’s incomplete. It answers how things run, not why they behave the way they do. The deeper question came up: Before the Event Loop even starts scheduling tasks, how does JavaScript know what those tasks are allowed to access? That’s where concepts like the compiler and lexical scope quietly enter the picture. JavaScript first reads the code and builds an understanding of it. Variable scope, function boundaries, and memory references are decided before execution begins. This is not the Event Loop’s responsibility. The Event Loop only works with what already exists. Lexical scope determines which variables belong to which functions. Closures decide what stays in memory even after a function finishes. None of this is created by the Event Loop, but all of it affects how async code behaves later. Data structures play a similar hidden role. The call stack is just a stack. Task queues are just queues. The scope chain behaves like a linked structure. The Event Loop doesn’t interpret logic. It simply moves execution between these structures based on a few strict rules. That discussion made one thing clear to me: If we don’t understand compiler behavior, lexical scoping, and basic data structures, the Event Loop will always feel confusing or “magical”. Async issues are rarely caused by the Event Loop itself. They usually come from misunderstanding scope, memory, or execution order. Once you see the Event Loop as a coordinator rather than a decision-maker, a lot of confusion disappears. #JavaScript #EventLoop #LexicalScope #Closures #AsyncProgramming #WebDevelopment #FrontendDevelopment #BackendDevelopment #FullStackDeveloper #SoftwareEngineering #ComputerScience #ProgrammingConcepts #DataStructures #DeveloperLearning #LearningInPublic #TechDiscussions #DeveloperCommunity #CodingLife #Debugging #EngineeringMindset #TechCareers
To view or add a comment, sign in
-
-
Mastering the Two Sum Algorithm in JavaScript (O(n) solution) Today I implemented the classic Two Sum problem using an optimized approach with Map in JavaScript. Instead of using the naive O(n²) double loop, I used a hash map to reduce the time complexity to O(n). Here is the core idea: 🔍 Why this approach is powerful Single loop → O(n) time complexity Constant-time lookup using a hash map Clean and scalable logic Interview-ready solution I'm currently strengthening my problem-solving and algorithmic thinking skills daily. You can find the full exercises repository here: 👉 https://lnkd.in/ej4fNeZs Consistency > Motivation. Small improvements every day compound over time. #JavaScript #Algorithms #ProblemSolving #WebDevelopment #100DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
Today I explored one of the most powerful and widely used concepts in JavaScript: Higher-Order Functions, along with the map, filter, and reduce array methods. Understanding higher-order functions helped me see how functions in JavaScript can be passed as arguments, returned from other functions, and used to write cleaner, more expressive code. The map, filter, and reduce methods showed how complex data transformations can be handled in a functional and readable way. What I learned: How higher-order functions enable reusable and composable logic Using map() to transform data without mutating the original array Using filter() to extract specific data based on conditions Using reduce() to accumulate values and solve complex problems in a single pass 💡 Key takeaway: These methods shift the focus from how to loop to what to compute, making code more declarative, readable, and maintainable. This session strengthened my ability to work with data efficiently and write cleaner JavaScript that scales well in real-world applications. #JavaScript #HigherOrderFunctions #MapFilterReduce #FunctionalProgramming #WebDevelopment #LearningJourney
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