Skip to main content

Posts

Showing posts from November, 2025

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"...

Scope in JavaScript

  Scope determines the accessibility of variables, functions, and objects in different parts of your code. In JavaScript, scope can be global, function, block, or module level . 1.1 Types of Scope 1.1.1 Global Scope Variables declared outside any function or block have global scope . Accessible from anywhere in the code. var globalVar = "I am global" ; function showVar ( ) { console . log (globalVar); // Accessible here } showVar (); console . log (globalVar); // Accessible here too 1.1.2 Function Scope Variables declared inside a function using var are function-scoped . Only accessible inside the function. function test ( ) { var x = 10 ; console . log (x); // 10 } console . log (x); // Error: x is not defined 1.1.3 Block Scope Variables declared with let or const inside {} are block-scoped . Only accessible inside that block. { let a = 5 ; const b = 10 ; console . log (a, b); // 5, 10 } console . log (a, b); // ...

✅ Summary: Why URLs give strings, useParams(), and parseInt()

1️⃣ Why URLs give strings? Everything in a URL is text only . When React Router reads a URL, it gives values like "12" , "100" as strings , not numbers. Example: URL: http://localhost:3000/user/15 15 → "15" (string) 2️⃣ What is useParams() ? useParams() is a React Router hook. It returns all dynamic values from the URL. Example: const { id } = useParams (); If your route is: < Route path= "/user/:id" element={ < User />}/> And your URL is /user/10 , then: id === "10" // string 3️⃣ Why do we use parseInt() with useParams() ? Since URL params come as strings , but sometimes we need numbers , we convert them: const { id } = useParams (); const userId = parseInt (id); // "10" → 10 ⭐ Final Combined Example import { useParams } from "react-router-dom" ; function User ( ) { const { id } = useParams (); // id = "5" const userId = parseIn...

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...