Skip to main content

1️⃣ Shallow Copy vs Deep Copy

 Yes! In JavaScript, when you perform a deep copy, completely new memory is allocated for the copied object or array, unlike a shallow copy where nested objects still share references. Let me explain in detail.



Shallow Copy

  • Only copies the first level of the object/array.

  • Nested objects/arrays are still shared between the original and the copy.

  • Changes in nested objects affect both original and copy.

let obj1 = { name: "Alice", address: { city: "NY" } }; let shallowCopy = { ...obj1 }; // or Object.assign({}, obj1) shallowCopy.address.city = "LA"; console.log(obj1.address.city); // Output: "LA" → nested object shared!

Notice that changing the nested object in shallowCopy also affects obj1.


Deep Copy

  • Creates a completely independent copy.

  • Nested objects/arrays are also copied to new memory locations.

  • Changing the copy does not affect the original.

let obj1 = { name: "Alice", address: { city: "NY" } }; // Using JSON method for deep copy let deepCopy = JSON.parse(JSON.stringify(obj1)); deepCopy.address.city = "LA"; console.log(obj1.address.city); // Output: "NY" → original remains unchanged

✅ Here, deepCopy has different memory for nested objects.


2️⃣ Other Ways to Deep Copy

  1. Using structuredClone (modern JS)

let deepCopy = structuredClone(obj1);
  1. Custom recursive function

function deepClone(obj) { if (obj === null || typeof obj !== "object") return obj; let clone = Array.isArray(obj) ? [] : {}; for (let key in obj) { clone[key] = deepClone(obj[key]); } return clone; }

✅ Key Point:

  • Shallow copy → shares memory for nested objects.

  • Deep copy → creates completely new memory for all levels.

Comments

Popular posts from this blog

Use Ref

  in case of useref no need to deal with vdom directly real dom of browser is updated Venice That's a fantastic observation, and you're touching on the core power of  useRef . You are  absolutely correct  that  useRef  allows you to bypass React's VDOM diffing process for direct DOM manipulation. However, let's add one layer of precision:  useRef  itself doesn't update the DOM.  It gives you a direct, stable reference  to  something (usually a DOM node), and  you  use that reference to update the DOM yourself, completely outside of React's normal render cycle. Let's contrast the two flows. The  useState  Flow (The "Official" React Way) This is the "ask for permission" model. Trigger:  You call  setCount() . Render:  React schedules a re-render. It calls your component, gets a new React Element tree, and builds a  new VDOM . Reconciliation (Diffing):  React compares the new VDOM to the old...

Ternary operator and its working on mechanism on array

  If condition is true → use valueIfTrue If condition is false → use valueIfFalse ✅ Your amenity example Your code: {amenity === 'wifi' ? 'WIFI' : amenity} Breakdown: ✔ Condition: amenity === 'wifi' This checks: “Is the current amenity equal to 'wifi'?” ✔ If condition is TRUE → show: 'WIFI' ✔ If condition is FALSE → show: amenity 🎯 Example with your amenitiesList const amenitiesList = [ 'wifi' , 'parking' , 'kitchen' ]; Looping through each item: 🔹 1. amenity = "wifi" Check condition: 'wifi' === 'wifi' → TRUE So ternary returns: 'WIFI' 👉 Display: WIFI 🔹 2. amenity = "parking" Check condition: 'parking' === 'wifi' → FALSE So ternary returns: amenity // which is "parking" 👉 Display: parking 🔹 3. amenity = "kitchen" Check condition: 'kitchen' === 'wifi' → FALS...

position vs translate

  The Golden Rule 🏠 Positioning ( top , left , etc.) 👉 Moves relative to the PARENT 🚶 Translate ( translate-x , translate-y ) 👉 Moves relative to ITSELF That’s it. If you remember only this, you’ll never confuse them again. 🎯 Imagine This Real-Life Story Think of a room (parent) and a table (element) . 1️⃣ left-1/2 → Parent Based You tell someone: “Put the table so its left edge is at 50% of the room.” The room decides the position. Room Width = 1000 px 50% = 500 px Table 's left edge is placed at 500px So positioning = 📏 Room measurement 2️⃣ translate-x-1/2 → Self Based Now you say: “Move the table forward by half of its own width.” The table measures itself. Table Width = 200px 50 % = 100px It moves 100px So translate = 📦 Self measurement 🔥 Ultimate Memory Formula Positioning → Parent controls me Translate → I control myself 💡 Why Centering Needs Both When you do: left - 1 / 2 You move the left edge to center. But the e...