<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MD ASHRAF</title>
    <description>The latest articles on DEV Community by MD ASHRAF (@md_ashraf_dev_to).</description>
    <link>https://dev.to/md_ashraf_dev_to</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3272389%2F1bdb9efe-70b8-490b-af3e-f11d9ff8829d.jpg</url>
      <title>DEV Community: MD ASHRAF</title>
      <link>https://dev.to/md_ashraf_dev_to</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/md_ashraf_dev_to"/>
    <language>en</language>
    <item>
      <title>Garbage Collection: Young Vs Old generation</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Wed, 26 Nov 2025 20:30:52 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/garbage-collection-young-vs-old-generation-4bkn</link>
      <guid>https://dev.to/md_ashraf_dev_to/garbage-collection-young-vs-old-generation-4bkn</guid>
      <description>&lt;h2&gt;
  
  
  Garbage collection?? Huhh it is just a small topic. wait what???? 😮😮
&lt;/h2&gt;

&lt;p&gt;Then what are these terms&lt;/p&gt;

&lt;h2&gt;
  
  
  Young Generation, Old Generatio, Eden, Mark and sweep etc.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. The Beginning: Empty Memory&lt;/strong&gt;&lt;br&gt;
Initially, our program's memory (the heap) is empty. We divide it into two main sections:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Young Generation (Green):&lt;/strong&gt; Where new objects are born. It's often smaller because most objects die quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Old Generation (Blue):&lt;/strong&gt; Where objects that have proven to live a long time go. This area is typically larger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7a02t2txa0pzwv3koa4v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7a02t2txa0pzwv3koa4v.png" alt="young vs old image" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Young generation structure:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Eden Space:&lt;/strong&gt; Where all new objects initially go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Survivor Space 1 (S1):&lt;/strong&gt; Objects surviving an Eden collection are moved here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Survivor Space 2 (S2):&lt;/strong&gt; Objects are moved between S1 and S2 in subsequent collections.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1n08kucarzgmzc2n09i0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1n08kucarzgmzc2n09i0.png" alt="Image young gen structure" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Collection Type:&lt;/strong&gt; The GC event that collects this area is called a &lt;strong&gt;Minor GC.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When Eden fills up, a Minor GC is triggered.&lt;/p&gt;

&lt;p&gt;Objects that are still reachable (in use) in Eden and the active Survivor space are copied to the other Survivor space.&lt;/p&gt;

&lt;p&gt;Objects that have survived a certain number of Minor GCs (known as the age threshold or tenuring threshold) are promoted (moved) to the Old Generation.&lt;/p&gt;

&lt;p&gt;The Eden and the initially active Survivor space are then completely cleared, making the process very fast.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Old Generation (Tenured)&lt;/strong&gt;&lt;br&gt;
This area holds objects that have survived many &lt;strong&gt;Minor GCs&lt;/strong&gt; in the Young Generation and have been &lt;strong&gt;promoted (or tenured)&lt;/strong&gt;. These are the &lt;code&gt;long-lived objects.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Collection Type:&lt;/strong&gt; The GC event that collects this area is called a &lt;strong&gt;Major GC&lt;/strong&gt; (or part of a Full GC, which usually collects the entire heap, including the Young Generation).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frequency:&lt;/strong&gt; It is collected much less frequently than the Young Generation because the objects here are more likely to still be in use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt; Major GC often uses algorithms optimized for reclaiming memory with a lower proportion of dead objects, such as** Mark-Sweep-Compact** (or variations like Concurrent Mark-Sweep or Garbage-First). These algorithms are generally slower but more thorough, as running them frequently would defeat the purpose of the generational model.&lt;/p&gt;




&lt;p&gt;🔄 &lt;strong&gt;Promotion/Tenuring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Promotion is the process where a long-lived object is moved from the &lt;strong&gt;Young Generation to the Old Generation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each time an object survives a Minor GC, its &lt;strong&gt;"age"&lt;/strong&gt; counter is incremented.&lt;/p&gt;

&lt;p&gt;Once the object's age reaches a specific tenuring threshold (which can be configured, often between 1 and 15), it is promoted to the Old Generation during the next Minor GC.&lt;/p&gt;

&lt;p&gt;This generational approach significantly improves GC performance by:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducing overhead:&lt;/strong&gt; Applying a &lt;strong&gt;fast, copy-based algorithm (like Copying Collector)&lt;/strong&gt; frequently to the small Young Generation (where most garbage is created).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrequently accessing the large Old Generation:&lt;/strong&gt; Only running the slower, more complex collection algorithms when necessary for the Old Generation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdq37gfkjm8ddvn7bef6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdq37gfkjm8ddvn7bef6q.png" alt=" " width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Summary Analogy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think of it like a post office sorting room:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ixz751vnoqrwusnj8ll.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ixz751vnoqrwusnj8ll.jpg" alt="Image summary" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow our more interview playlists here: 📌📌📌&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/md_ashraf_dev_to/series/34139"&gt;Java Interview series&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/series/32353"&gt;Html &amp;amp; CSS Interview series&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/series/32283"&gt;Javascript Interview series&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/series/32269"&gt;Angular Interview series&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>garbage</category>
      <category>interview</category>
      <category>development</category>
    </item>
    <item>
      <title>The Diamond Problem in JAVA 💎 💎 💎 💎 💎 💎 💎</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Fri, 21 Nov 2025 11:33:36 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/the-diamond-problem-in-java-567d</link>
      <guid>https://dev.to/md_ashraf_dev_to/the-diamond-problem-in-java-567d</guid>
      <description>&lt;h2&gt;
  
  
  The Diamond Problem is an ambiguity issue in multiple inheritance where a class inherits the same method from two or more parent classes. Java prevents this with classes, but it can arise with interfaces (since Java 8) if a class implements two interfaces that provide a default method with the same signature.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrpvwdk6mdmf5oisc9he.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrpvwdk6mdmf5oisc9he.png" alt="Problem diagram" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Interface 1
interface InterfaceA {
    default void sameMethod() {
        // some functionality
        System.out.println("Implementation from Interface A");
    }
}

// Interface 2
interface InterfaceB {
    default void sameMethod() {
        // some functionality
        System.out.println("Implementation from Interface B");
    }
}

// Class D implements both interfaces, causing the Diamond Problem
// This code will result in a **COMPILATION ERROR

class ClassD implements InterfaceA, InterfaceB {
    // Compile-time error:
    // 'ClassD inherits unrelated defaults for sameMethod() from types InterfaceA and InterfaceB'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;The Solution (After Resolution)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java resolves the Diamond Problem by forcing the implementing class to provide its own version of the ambiguous method. This is Java's &lt;strong&gt;rule for default method ambiguity:&lt;/strong&gt; when there's a conflict, the implementing class must &lt;strong&gt;override the method&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Interface 1 (Same as before)
interface InterfaceA {
    default void sameMethod() {
        System.out.println("Implementation from Interface A");
    }
}

// Interface 2 (Same as before)
interface InterfaceB {
    default void sameMethod() {
        System.out.println("Implementation from Interface B");
    }
}

// Class D resolves the Diamond Problem by providing its own implementation
class ClassD implements InterfaceA, InterfaceB {

    // Must override the ambiguous method
    @Override
    public void sameMethod() {
        // Option 1: Provide a brand new implementation
        System.out.println("Implementation from Class D (Resolving conflict)");
    }
}

public class DiamondProblemDemo {
    public static void main(String[] args) {
        ClassD obj = new ClassD();
        obj.sameMethod(); // Output: Implementation from Class D (Resolving conflict)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;strong&gt;child class&lt;/strong&gt; have provided its own implementation, but suppose sometimes we want to use the &lt;strong&gt;implementation already defined in one of the interface&lt;/strong&gt; above then we can &lt;strong&gt;bring that implememntation&lt;/strong&gt; inside child class **overridden method **using&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Option 2 (Advanced): Call a specific interface's default method&lt;br&gt;
        // **InterfaceA.super.sameMethod()**;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Class D resolves the Diamond Problem by providing its own implementation
class ClassD implements InterfaceA, InterfaceB {

    // Must override the ambiguous method
    @Override
    public void sameMethod() {
        // Option 1: Provide a brand new implementation
        System.out.println("Implementation from Class D (Resolving conflict)");

        // Option 2 (Advanced): Call a specific interface's default method
        // InterfaceA.super.sameMethod();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lets explore more deeper
&lt;/h2&gt;

&lt;p&gt;If InterfaceA extends two other interfaces (InterfaceX and InterfaceY), and both InterfaceX and InterfaceY have a default method with the same signature, the ambiguity is solved at which level?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; At the level of InterfaceA itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The rule is:&lt;/strong&gt; &lt;code&gt;A class or interface always wins over a default method from a super-interface.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Example problem:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface InterfaceX {
    default void sameMethod() {
        System.out.println("From X");
    }
}

interface InterfaceY {
    default void sameMethod() {
        System.out.println("From Y");
    }
}

// THIS WILL RESULT IN A COMPILATION ERROR!
// Interface A inherits unrelated defaults for sameMethod() from types InterfaceX and InterfaceY.
// It must explicitly override sameMethod().
interface InterfaceA extends InterfaceX, InterfaceY {
    // Error occurs here because Java doesn't know whether A should use X's or Y's version.
}

class ClassD implements InterfaceA {
    // This class won't even compile because InterfaceA has an error.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Resolution (Required Override in InterfaceA)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fix this, &lt;code&gt;InterfaceA must provide an implementation for sameMethod().&lt;/code&gt; It can use one of the parent implementations or define a new one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface InterfaceX {
    default void sameMethod() {
        System.out.println("From X");
    }
}

interface InterfaceY {
    default void sameMethod() {
        System.out.println("From Y");
    }
}

// RESOLUTION: InterfaceA provides an override.
interface InterfaceA extends InterfaceX, InterfaceY {
    @Override
    default void sameMethod() {
        // InterfaceA chooses which method to use, or provides its own.
        // Here, it chooses to call the implementation from InterfaceX.
        InterfaceX.super.sameMethod();
        System.out.println("...via A's resolution.");

        // 2. Explicitly call Interface Y's implementation
        InterfaceY.super.sameMethod();
    }
}

// Class D can now implement InterfaceA without error
class ClassD implements InterfaceA {
    // Class D doesn't need to do anything, as InterfaceA has already provided the concrete method.
}

public class DiamondProblemDeepDive {
    public static void main(String[] args) {
        ClassD obj = new ClassD();
        obj.sameMethod();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠&lt;strong&gt;Summary of Hierarchy Rules&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class always wins:&lt;/strong&gt; A method defined in a class (like ClassD) is always chosen over a default method in any interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sub-interface wins over super-interface:&lt;/strong&gt; A default method in a sub-interface (like InterfaceA overriding the method) is always chosen over a default method in a super-interface (InterfaceX or InterfaceY).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conflict = Mandatory Override:&lt;/strong&gt; If an interface or class inherits two or more conflicting default methods from its immediate parents, it must provide an explicit override (either by defining a new body or by using the InterfaceName.super.methodName() syntax).&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you, PLease Subscribe like and bookmark our more series here .
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/md_ashraf_dev_to/series/32353"&gt;HTML &amp;amp; CSS Series&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/series/32283"&gt;Javascript Series&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/series/32269"&gt;Angular Series&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>inheritance</category>
      <category>ambiguity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Switch images in desktop and mobile views using CSS: Media queries</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Sat, 12 Jul 2025 02:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/switch-images-in-desktop-and-mobile-views-using-css-media-queries-3h8f</link>
      <guid>https://dev.to/md_ashraf_dev_to/switch-images-in-desktop-and-mobile-views-using-css-media-queries-3h8f</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wfgjfu8wehx033itftk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wfgjfu8wehx033itftk.jpg" alt="Image multi views" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To switch images in desktop and mobile view using CSS media queries, you can use the following approach:&lt;/p&gt;

&lt;p&gt;HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="desktop-image.jpg" class="desktop-image" alt="Desktop Image"&amp;gt;
&amp;lt;img src="mobile-image.jpg" class="mobile-image" alt="Mobile Image"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.mobile-image {
  display: none;
}

@media only screen and (max-width: 768px) {
  .desktop-image {
    display: none;
  }
  .mobile-image {
    display: block;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;.desktop-image&lt;/code&gt; is displayed by default, and the &lt;code&gt;.mobile-image&lt;/code&gt; is hidden. When the screen width is 768px or less ( typical mobile screen width), the &lt;code&gt;.desktop-image&lt;/code&gt; is hidden, and the &lt;code&gt;.mobile-image&lt;/code&gt; is displayed.&lt;/p&gt;




&lt;p&gt;Alternatively, you can use the &lt;code&gt;picture&lt;/code&gt; element and &lt;code&gt;srcset&lt;/code&gt; attribute to achieve the same result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;picture&amp;gt;
  &amp;lt;source srcset="mobile-image.jpg" media="(max-width: 768px)"&amp;gt;
  &amp;lt;img src="desktop-image.jpg" alt="Desktop Image"&amp;gt;
&amp;lt;/picture&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach allows the browser to choose the correct image based on the screen width, without needing to use CSS media queries.&lt;/p&gt;




&lt;p&gt;You can also use CSS background images and media queries to achieve the same result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.image-container {
  background-image: url('desktop-image.jpg');
}

@media only screen and (max-width: 768px) {
  .image-container {
    background-image: url('mobile-image.jpg');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that the image container element should have a &lt;strong&gt;fixed height and width&lt;/strong&gt; for this approach to work.&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>learning</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Unit test CHILD component from PARENT component's test case</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Fri, 11 Jul 2025 08:58:50 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/unit-test-child-component-from-parent-components-test-case-od2</link>
      <guid>https://dev.to/md_ashraf_dev_to/unit-test-child-component-from-parent-components-test-case-od2</guid>
      <description>&lt;p&gt;Here are some ways to get a child component in a parent test case in Angular:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using&lt;/strong&gt; &lt;code&gt;fixture.debugElement.query()&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const childDebugElement = fixture.debugElement.query(By.directive(ChildComponent));
const childComponent = childDebugElement.componentInstance;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method uses the &lt;code&gt;debugElement&lt;/code&gt; property of the fixture to query for the child component.&lt;/p&gt;

&lt;p&gt;2.Using &lt;code&gt;fixture.debugElement.queryAll()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const childDebugElements = fixture.debugElement.queryAll(By.directive(ChildComponent));
const childComponents = childDebugElements.map(de =&amp;gt; de.componentInstance);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method uses the &lt;code&gt;queryAll&lt;/code&gt; method to retrieve an array of debug elements that match the child component directive.&lt;/p&gt;

&lt;p&gt;3.Using &lt;code&gt;fixture.nativeElement.querySelector()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const childElement = fixture.nativeElement.querySelector('app-child');
const childComponent = fixture.debugElement.query(By.css('app-child')).componentInstance;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method uses the &lt;code&gt;nativeElement&lt;/code&gt; property of the fixture to query for the child component element, and then uses the &lt;code&gt;debugElement&lt;/code&gt; property to retrieve the component instance.&lt;/p&gt;

&lt;p&gt;4.&lt;em&gt;Using a test double or spy&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const childComponentSpy = jasmine.createSpyObj('ChildComponent', ['methodName']);
TestBed.configureTestingModule({
  declarations: [ParentComponent],
  providers: [{ provide: ChildComponent, useValue: childComponentSpy }]
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method creates a test double or spy for the child component, allowing you to test the parent component's interactions with the child component without actually rendering the child component.&lt;/p&gt;

&lt;p&gt;5.&lt;em&gt;Using &lt;code&gt;ViewChild&lt;/code&gt;&lt;/em&gt;:&lt;br&gt;
// parent.component.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ViewChild(ChildComponent) childComponent: ChildComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// parent.component.spec.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const childComponent = fixture.componentInstance.childComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method uses the &lt;code&gt;ViewChild&lt;/code&gt; decorator to retrieve a reference to the child component instance in the parent component.&lt;/p&gt;

&lt;p&gt;These are just a few examples of how you can get a child component in a parent test case in Angular. The best approach will depend on your specific testing needs and requirements.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>unittest</category>
      <category>learning</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Set css style using html data attribute</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Fri, 04 Jul 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/set-css-style-using-html-data-attribute-53dc</link>
      <guid>https://dev.to/md_ashraf_dev_to/set-css-style-using-html-data-attribute-53dc</guid>
      <description>&lt;h2&gt;
  
  
  Using attr() function
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Here's the syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;element {
  property: attr(data-attribute-name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, if you have an HTML element like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div data-color="#ff0000"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can access the &lt;code&gt;data-color&lt;/code&gt; attribute in CSS like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  background-color: attr(data-color);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this will only work for certain properties like &lt;code&gt;content&lt;/code&gt;, and not for properties like &lt;code&gt;background-color&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to use the data attribute value for styling purposes, you can use this way:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;style.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a{
  background-color: attr(data-bg-color type(&amp;lt;color&amp;gt;)); /*add type(&amp;lt;color&amp;gt;) as it is, to tell browser that type of value is a type of color*/
  font-size: attr(data-font-size px)}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="https://www.w3schools.com" data-font-size="30" data-bg-color="#ff0000"&amp;gt;Visit W3Schools&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Solution 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also use CSS custom properties (variables) to achieve this. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div style="--color: #ff0000;"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  background-color: var(--color);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, you can dynamically set the value of the &lt;code&gt;--color&lt;/code&gt; property and it will be used in the CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; that the &lt;code&gt;attr()&lt;/code&gt; function is not widely supported for all properties, and it's mainly used for the &lt;code&gt;content&lt;/code&gt; property. For other properties, you may need to use other approaches like attribute selectors or custom. &lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>web</category>
      <category>learning</category>
    </item>
    <item>
      <title>Array Mutating &amp; Non-Mutating Methods</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Thu, 03 Jul 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/array-mutating-non-mutating-methods-cga</link>
      <guid>https://dev.to/md_ashraf_dev_to/array-mutating-non-mutating-methods-cga</guid>
      <description>&lt;p&gt;Lets first see the list of function categorised as &lt;code&gt;Mutating Methods &amp;amp; Non-Mutating Methods below&lt;/code&gt;, will then share example of each function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-----------------------+---------------------------+
| Mutating Methods      | Non-Mutating Methods      |
+-----------------------+---------------------------+
| push()                | concat()                  |
| pop()                 | slice()                   |
| shift()               | map()                     |
| unshift()             | filter()                  |
| splice()              | reduce()                  |
| sort()                | reduceRight()             |
| reverse()             | forEach()                 |
| fill()                | every()                   |
| copyWithin()          | some()                    |
|                       | find()                    |
|                       | findIndex()               |
|                       | findLast() (ES2023)       |
|                       | findLastIndex() (ES2023)  |
|                       | includes()                |
|                       | indexOf()                 |
|                       | lastIndexOf()             |
|                       | join()                    |
|                       | toString()                |
|                       | toLocaleString()          |
|                       | flat()                    |
|                       | flatMap()                 |
|                       | at() (ES2022)             |
|                       | toReversed() (ES2023)     |
|                       | toSorted() (ES2023)       |
|                       | toSpliced() (ES2023)      |
|                       | with() (ES2023)           |
|                       | Array.from()              |
|                       | Array.isArray()           |
|                       | Array.of()                |
|                       | Object.groupBy() (ES2024) |
|                       | Map.groupBy() (ES2024)    |
+-----------------------+---------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Array Methods that Mutate the Original Array:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;push()&lt;/code&gt;: Adds one or more elements to the end of an array and returns the new length of the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
arr.push(4); // arr is now [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pop()&lt;/code&gt;: Removes the last element from an array and returns that element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let removed = arr.pop(); // removed is 3, arr is now [1, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;shift():&lt;/code&gt; Removes the first element from an array and returns that element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let removed = arr.shift(); // removed is 1, arr is now [2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unshift():&lt;/code&gt; Adds one or more elements to the beginning of an array and returns the new length of the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
arr.unshift(0); // arr is now [0, 1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;splice():&lt;/code&gt; Adds, removes, or replaces elements in an array at a specified index, and returns an array containing the deleted elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let removed = arr.splice(2, 1, 6, 7); // removed is [3], arr is now [1, 2, 6, 7, 4, 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sort():&lt;/code&gt; Sorts the elements of an array in place and returns the reference to the same array, now sorted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [3, 1, 4, 1, 5];
arr.sort(); // arr is now [1, 1, 3, 4, 5] (default string sort, or with a compare function)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reverse():&lt;/code&gt; Reverses the order of the elements in an array in place and returns the reference to the same array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
arr.reverse(); // arr is now [3, 2, 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fill():&lt;/code&gt; Fills all the elements of an array from a start index to an end index with a static value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
arr.fill(0, 2, 4); // arr is now [1, 2, 0, 0, 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;copyWithin():&lt;/code&gt; Copies part of an array to another location in the same array and returns the modified array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3); // arr is now [4, 5, 3, 4, 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Array Methods that Do Not Mutate the Original Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These methods return a new array or a new value, leaving the original array unchanged.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;concat():&lt;/code&gt; Joins two or more arrays and returns a new array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let arr1 = [1, 2];
let arr2 = [3, 4];
let newArr = arr1.concat(arr2); // newArr is [1, 2, 3, 4], arr1 and arr2 are unchanged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;slice():&lt;/code&gt; Returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let newArr = arr.slice(1, 4); // newArr is [2, 3, 4], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;map():&lt;/code&gt; Creates a new array populated with the results of calling a provided function on every element in the calling array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let newArr = arr.map(x =&amp;gt; x * 2); // newArr is [2, 4, 6], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;filter():&lt;/code&gt; Creates a new array with all elements that pass the test implemented by the provided function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let newArr = arr.filter(x =&amp;gt; x % 2 === 0); // newArr is [2, 4], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reduce():&lt;/code&gt; Executes a reducer function (that you provide) on each element of the array, resulting in a single output value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let sum = arr.reduce((acc, current) =&amp;gt; acc + current, 0); // sum is 6, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reduceRight():&lt;/code&gt; Similar to reduce(), but executes the reducer function from right to left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let sum = arr.reduceRight((acc, current) =&amp;gt; acc + current, 0); // sum is 6, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;forEach():&lt;/code&gt; Executes a provided function once for each array element. (Returns undefined, does not create a new array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
arr.forEach(x =&amp;gt; console.log(x)); // arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;every():&lt;/code&gt; Tests whether all elements in the array pass the test implemented by the provided function. Returns a Boolean value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let allEven = arr.every(x =&amp;gt; x % 2 === 0); // allEven is false, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;some():&lt;/code&gt; Tests whether at least one element in the array passes the test implemented by the provided function. Returns a Boolean value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let hasEven = arr.some(x =&amp;gt; x % 2 === 0); // hasEven is true, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;find():&lt;/code&gt; Returns the value of the first element in the provided array that satisfies the provided testing function. Otherwise, undefined is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let found = arr.find(x =&amp;gt; x &amp;gt; 3); // found is 4, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findIndex():&lt;/code&gt; Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, -1 is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let index = arr.findIndex(x =&amp;gt; x &amp;gt; 3); // index is 3, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findLast() (ES2023):&lt;/code&gt; Returns the value of the last element in the array that satisfies the provided testing function. Otherwise, undefined is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 3];
let found = arr.findLast(x =&amp;gt; x === 3); // found is 3, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findLastIndex() (ES2023):&lt;/code&gt; Returns the index of the last element in the array that satisfies the provided testing function. Otherwise, -1 is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 3];
let index = arr.findLastIndex(x =&amp;gt; x === 3); // index is 4, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;includes():&lt;/code&gt; Determines whether an array includes a certain value among its entries, returning true or false as appropriate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let hasTwo = arr.includes(2); // hasTwo is true, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;indexOf():&lt;/code&gt; Returns the first index at which a given element can be found in the array, or -1 if it is not present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let index = arr.indexOf(2); // index is 1, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;lastIndexOf():&lt;/code&gt; Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 2];
let index = arr.lastIndexOf(2); // index is 3, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;join():&lt;/code&gt; Creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let str = arr.join('-'); // str is "1-2-3", arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;toString():&lt;/code&gt; Returns a string representing the specified array and its elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let str = arr.toString(); // str is "1,2,3", arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;toLocaleString():&lt;/code&gt; Returns a string representing the elements of the array. The elements are converted to strings using their toLocaleString methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1000, 200];
let str = arr.toLocaleString('en-US'); // str is "1,000,200", arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;flat():&lt;/code&gt; Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, [2, 3], [4, [5]]];
let newArr = arr.flat(2); // newArr is [1, 2, 3, 4, 5], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;flatMap():&lt;/code&gt; Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let newArr = arr.flatMap(x =&amp;gt; [x, x * 2]); // newArr is [1, 2, 2, 4, 3, 6], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;at() (ES2022):&lt;/code&gt; Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let element = arr.at(-1); // element is 3, arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;toReversed() (ES2023):&lt;/code&gt; Returns a new array with the elements in reversed order. This is the non-mutating counterpart of reverse().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let newArr = arr.toReversed(); // newArr is [3, 2, 1], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;toSorted() (ES2023):&lt;/code&gt; Returns a new array with the elements sorted in ascending order. This is the non-mutating counterpart of sort().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [3, 1, 4, 1, 5];
let newArr = arr.toSorted(); // newArr is [1, 1, 3, 4, 5], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;toSpliced() (ES2023):&lt;/code&gt; Returns a new array with some elements removed and/or replaced at a given index. This is the non-mutating counterpart of splice().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5];
let newArr = arr.toSpliced(2, 1, 6, 7); // newArr is [1, 2, 6, 7, 4, 5], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;with() (ES2023):&lt;/code&gt; Returns a new array with the element at the given index replaced with the given value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
let newArr = arr.with(1, 99); // newArr is [1, 99, 3], arr is unchanged

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.from():&lt;/code&gt; Creates a new, shallow-copied Array instance from an array-like or iterable object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "abc";
let arr = Array.from(str); // arr is ['a', 'b', 'c']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.isArray():&lt;/code&gt; Determines whether the passed value is an Array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.isArray([1, 2, 3]); // true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Array.of():&lt;/code&gt; Creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.of(1, 2, 3); // [1, 2, 3]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Object.groupBy() (ES2024):&lt;/code&gt; This is a static method that groups the elements of an iterable according to the string values returned by a provided callback function. It returns an object with the groups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 30 }];
const groupedByAge = Object.groupBy(array, ({ age }) =&amp;gt; age);
// groupedByAge will be:
// {
//   '30': [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 30 }],
//   '25': [{ name: 'Bob', age: 25 }]
// }
// The original array is not mutated.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Map.groupBy() (ES2024):&lt;/code&gt; Similar to Object.groupBy(), but returns a Map instance instead of an object. This is useful when the keys for grouping might not be valid object keys (e.g., symbols, or numbers that you want to keep as numbers).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [{ type: 'fruit', name: 'apple' }, { type: 'vegetable', name: 'carrot' }, { type: 'fruit', name: 'banana' }];
const groupedByType = Map.groupBy(array, ({ type }) =&amp;gt; type);
// groupedByType will be a Map:
// Map {
//   'fruit' =&amp;gt; [{ type: 'fruit', name: 'apple' }, { type: 'fruit', name: 'banana' }],
//   'vegetable' =&amp;gt; [{ type: 'vegetable', name: 'carrot' }]
// }
// The original array is not mutated.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The introduction of &lt;code&gt;toReversed()&lt;/code&gt;, &lt;code&gt;toSorted()&lt;/code&gt;, and &lt;code&gt;toSpliced()&lt;/code&gt; in ES2023 (and now part of ES2024 standard) is a significant step towards encouraging immutability in JavaScript array operations, providing non-mutating alternatives to their long-standing mutating counterparts. &lt;/p&gt;

&lt;p&gt;📌📌 Moe Javascript related topics and interview questions here:&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/tricky-javascript-codes-part-3-1mkh"&gt;Tricky javascript code part 3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>programming</category>
      <category>web</category>
    </item>
    <item>
      <title>Custom pipe to format numbers in Angular</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Wed, 02 Jul 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/custom-pipe-to-format-numbers-in-angular-2og2</link>
      <guid>https://dev.to/md_ashraf_dev_to/custom-pipe-to-format-numbers-in-angular-2og2</guid>
      <description>&lt;p&gt;Here we are creating custom pipe to format large numbers (e.g., 1,000,000 → 1M).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;pipe.ts&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'largeNumberFormat'
})
export class LargeNumberFormatPipe implements PipeTransform {
  transform(value: number): string {
    if (value &amp;gt;= 1000000) { // dividing number by 10L since 1M == 10L
      return (value / 1000000).toFixed(1) + 'M'; // toFixed(1) rounding till one decimal value
    } else if (value &amp;gt;= 1000) {
      return (value / 1000).toFixed(1) + 'K';
    } else {
      return value.toString(); // returning number itself in string format if less than thousand.
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage in &lt;strong&gt;&lt;u&gt;component.html&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;{{ 1000000 | largeNumberFormat }}&amp;lt;/p&amp;gt; // Output: 1.0M
&amp;lt;p&amp;gt;{{ 1000 | largeNumberFormat }}&amp;lt;/p&amp;gt; // Output: 1.0K
&amp;lt;p&amp;gt;{{ 500 | largeNumberFormat }}&amp;lt;/p&amp;gt; // Output: 500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; By default pipe is pure, meaning they only execute when the &lt;code&gt;input value changes&lt;/code&gt;. If you want the pipe to execute on &lt;code&gt;every change detection cycle&lt;/code&gt;, you can make it &lt;code&gt;impure&lt;/code&gt; by setting the &lt;code&gt;pure&lt;/code&gt; property to &lt;strong&gt;false&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
@Pipe({
  name: 'largeNumberFormat',
  pure: false
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When is an impure pipe useful?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An impure pipe is useful when the pipe's output depends on something other than the input value, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Current time:&lt;/strong&gt; If you want to display the time elapsed since a certain date, you would need an impure pipe to update the output every second.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Random numbers:&lt;/strong&gt; If you want to generate a random number based on the input value, an impure pipe would be necessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;External data:&lt;/strong&gt; If the pipe's output depends on external data that can change outside of the component's control, an impure pipe might be necessary.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, keep in mind that &lt;code&gt;**impure pipes can have performance implications**&lt;/code&gt;, as they are executed on every change detection cycle. Therefore, use them judiciously and only when necessary.&lt;/p&gt;

&lt;p&gt;📌📌 More Angular interview related stuff here:&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/rxjs-operators-and-angular-bonding-15kl"&gt;Rxjs and Angular bonding&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>learning</category>
      <category>web</category>
    </item>
    <item>
      <title>Cancelling HTTP request when Angular Component destroyed</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Tue, 01 Jul 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/cancelling-http-request-when-angular-component-destroyed-582</link>
      <guid>https://dev.to/md_ashraf_dev_to/cancelling-http-request-when-angular-component-destroyed-582</guid>
      <description>&lt;h2&gt;
  
  
  To cancel an ongoing HTTP request when a component is destroyed, you can use the following techniques:
&lt;/h2&gt;




&lt;ol&gt;
&lt;li&gt;Using &lt;strong&gt;&lt;code&gt;takeUntil&lt;/code&gt;&lt;/strong&gt; operator:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-example',
  template: '&amp;lt;p&amp;gt;Example Component&amp;lt;/p&amp;gt;',
})
export class ExampleComponent implements OnDestroy {
  private ngUnsubscribe = new Subject&amp;lt;void&amp;gt;();

  constructor(private http: HttpClient) {
    this.http.get('https://api.example.com/data') // making http call
      .pipe(takeUntil(this.ngUnsubscribe)) // will subscribe to observanle until "ngUnsubscribe" is complete
      .subscribe((response) =&amp;gt; {
        console.log(response);
      });
  }

  ngOnDestroy(): void {
    //marking ngUnsubscribe observable complete will unsubscribe the observable and request will get cancelled.
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;2.Using &lt;strong&gt;&lt;code&gt;Subscription&lt;/code&gt;&lt;/strong&gt; object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-example',
  template: '&amp;lt;p&amp;gt;Example Component&amp;lt;/p&amp;gt;',
})
export class ExampleComponent implements OnDestroy {
  private subscription: Subscription;

  constructor(private http: HttpClient) {
    this.subscription = this.http.get('https://api.example.com/data')
      .subscribe((response) =&amp;gt; {
        console.log(response);
      });
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However this approach is good for a single subscription if we have &lt;strong&gt;(n)&lt;/strong&gt; subscriptions and we want to &lt;strong&gt;&lt;code&gt;cancel all on component destroy&lt;/code&gt;&lt;/strong&gt;(which is &lt;strong&gt;good practice&lt;/strong&gt;), follow &lt;strong&gt;solution 1&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Why is it important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Canceling ongoing HTTP requests&lt;/strong&gt; when a component is destroyed is important to &lt;code&gt;prevent memory leaks&lt;/code&gt; and &lt;code&gt;improve the overall performance&lt;/code&gt; of your application. When a component is destroyed, any ongoing HTTP requests associated with that component should be cancelled to prevent unnecessary resource usage and potential errors.&lt;/p&gt;

&lt;p&gt;📌📌 More Angular related topics here:&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/rxjs-operators-and-angular-bonding-15kl"&gt;Rxjs and Angular bonding&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>http</category>
      <category>learning</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>Tricky javascript codes part 3</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Mon, 30 Jun 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/tricky-javascript-codes-part-3-1mkh</link>
      <guid>https://dev.to/md_ashraf_dev_to/tricky-javascript-codes-part-3-1mkh</guid>
      <description>&lt;ul&gt;
&lt;li&gt;We have 3 nested boxes (box1, box2, box3). When clicking each box, log its ID to the console. Clicking an inner box should NOT trigger clicks on outer boxes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnby7p4obwiuoxhyjfvpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnby7p4obwiuoxhyjfvpz.png" alt="Image box" width="342" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is this question about?&lt;/strong&gt;&lt;br&gt;
This question is about &lt;strong&gt;&lt;code&gt;event handling and event propagation&lt;/code&gt;&lt;/strong&gt; in JavaScript, specifically how to control the flow of click events when you have nested HTML elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The core problem&lt;/strong&gt; it addresses is how to prevent a click on an inner element from also triggering the click events of its parent (outer) elements. This is a common scenario in web development where you want specific actions to happen only when a particular element is clicked, and not its containers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: There are many ways to do the same but the more optimisez way is Wrapping all the divs inside one &lt;strong&gt;parent div&lt;/strong&gt; having id as &lt;code&gt;container&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then adding event listener&lt;/strong&gt; on this parent element&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.html file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="container"&amp;gt;
    &amp;lt;div id="box1" class="box"&amp;gt;
      box1
      &amp;lt;div id="box2" class="box"&amp;gt;
         box2
         &amp;lt;div id="box3" class="box"&amp;gt;box3&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.css file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.box {
  min-width: 200px;
  width: fit-content; // to match width of div till its content
  border: 1px solid #000;
  padding: 15px;
}

#box1 {
  background-color: yellow;
  padding: 10px;
}
#box2 {
  background-color: cyan;
  padding: 10px;
}
#box3 {
  background-color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;script file.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('container').addEventListener('click', event =&amp;gt; {
  // Todo on click of container element or any of its child
  if (event.target.classList.contains('box')) {
    /* checking if click happens only on element having class as box, as we don't need to handle click for all other children of container as of now, instead of box.*/

    console.log('id is: ', event.target.id);
    event.stopPropagation(); // preventing event from propagation to parent on click of child element
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://onecompiler.com/html/43p72rnxa" rel="noopener noreferrer"&gt;Live example here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt;: As I have said earlier there can be many ways to solve this  problem, we can add &lt;code&gt;event listener&lt;/code&gt; to each individual &lt;code&gt;divs&lt;/code&gt;also, because only 3 &lt;code&gt;divs&lt;/code&gt; are there for now, but if no. will be more then we will need &lt;code&gt;loop&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;So to avoid &lt;code&gt;loop&lt;/code&gt;, we have taken parent element. we can take &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; also as a parent element, but then script will search to a wider aread of code and will be time consuming. Thus we have narrowed down the scope of event listener to a small area which is a &lt;strong&gt;div with id as container&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;📌📌 More Javascript interview related code here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/md_ashraf_dev_to/tricky-javascript-code-part-2-3f6n"&gt;Tricky Javascript code part2 &lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>html</category>
      <category>learning</category>
    </item>
    <item>
      <title>Angular Directive that detects click outside of its host element</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Sun, 29 Jun 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/angular-directive-that-detects-click-outside-of-its-host-element-5hia</link>
      <guid>https://dev.to/md_ashraf_dev_to/angular-directive-that-detects-click-outside-of-its-host-element-5hia</guid>
      <description>&lt;p&gt;&lt;strong&gt;Approach:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Angular directive needs to listen for click events on the entire document.&lt;/li&gt;
&lt;li&gt;Then check if the clicked element is within its host element or not.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;click-outside.directive.ts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0s6ksors3jbylgf8zp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0s6ksors3jbylgf8zp8.png" alt="Image custom directive" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's break down the code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Directive({ selector: '[appClickOutside]' }):&lt;/code&gt; This means you'll apply the directive to an HTML element like this: &lt;code&gt;&amp;lt;div appClickOutside&amp;gt;...&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@HostListener('document:click', ['$event.target']):&lt;/code&gt; This tells Angular to listen for the &lt;code&gt;click&lt;/code&gt; event on the entire document object, not just the host element itself. This is crucial for detecting clicks outside.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;['$event.target']:&lt;/code&gt; This tells Angular to pass the target property of the click event (which is the actual DOM element that was clicked) as an argument to our &lt;code&gt;onClick&lt;/code&gt;method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const clickedInside = this.elementRef.nativeElement.contains(targetElement);&lt;/code&gt; : This line uses the &lt;code&gt;contains()&lt;/code&gt; method of the DOM Node interface. It checks if the &lt;code&gt;targetElement&lt;/code&gt; (the element that was clicked) is a descendant of the directive's host element (&lt;code&gt;this.elementRef.nativeElement&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Use It:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Define in module:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ClickOutsideDirective } from './click-outside.directive'; // Import your directive

@NgModule({
  declarations: [
    AppComponent,
    ClickOutsideDirective // &amp;lt;---  Declare your directive
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;component.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div style="border: 2px solid blue; padding: 20px; margin: 50px;"
  appClickOutside   &amp;lt;-- THIS IS WHERE THE DIRECTIVE IS APPLIED --&amp;gt;
  clickOutside)="onClickedOutside()"&amp;gt;  &amp;lt;-- THIS IS WHERE THE COMPONENT LISTENS TO THE DIRECTIVE'S OUTPUT --&amp;gt;
  This is the host element. Click anywhere outside this blue box!
  &amp;lt;button (click)="onButtonClick($event)"&amp;gt;Click Me (Inside)&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;p *ngIf="message"&amp;gt;{{ message }}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handling event on click in &lt;strong&gt;component.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  message: string = '';

  onClickedOutside(): void {
    this.message = 'Clicked outside the box!';
    console.log('Clicked outside the element!');
  }

  onButtonClick(event: Event): void {
    event.stopPropagation(); // Prevent the document click listener from firing immediately
    this.message = 'Button inside clicked!';
    console.log('Button inside clicked!');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌📌 More angular learnings here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/md_ashraf_dev_to/rxjs-operators-and-angular-bonding-15kl"&gt;RXJS and Angular Bonding&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/http-requests-in-angular-21jj"&gt;Angular HTTP integration&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/xss-attacks-and-angular-handling-techniques-23n2"&gt;XSS Attack and Angular Handling&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>angular</category>
      <category>learning</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Tricky Javascript Code part 2</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Sat, 28 Jun 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/tricky-javascript-code-part-2-3f6n</link>
      <guid>https://dev.to/md_ashraf_dev_to/tricky-javascript-code-part-2-3f6n</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Find and print the name(s) of user(s) whose age is greater than 30 in nested object. In javascript&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vmrvw167lg21abcrm83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vmrvw167lg21abcrm83.png" alt="Object question image" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;javascript file&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv83pevuvvmf1wsc82hk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv83pevuvvmf1wsc82hk.png" alt="Q1 code image" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7q80yous0qqgqgyv0zia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7q80yous0qqgqgyv0zia.png" alt="Image q1 output" width="770" height="133"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;2.Flatten an array &lt;code&gt;(input: [1,2,[3,4], 5], output: [1,2,3,4,5])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 1:&lt;/strong&gt; using &lt;code&gt;flat()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, [3, 4], 5];
const flattenedArr = arr.flat(); 
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but it will not flatten &lt;strong&gt;multiple Levels&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 2:&lt;/strong&gt; using &lt;code&gt;flat()&lt;/code&gt; with &lt;code&gt;Infinity&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, [3, 4, [5, 6]], 7];
const flattenedArr = arr.flat(Infinity);
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution 3:&lt;/strong&gt; using &lt;code&gt;recursion&lt;/code&gt; and &lt;code&gt;for loop&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function flattenArray(arr) {
  let result = [];
  arr.forEach(item =&amp;gt; {
    if (Array.isArray(item)) {
      result = result.concat(flattenArray(item));
      // ---------- OR ------
      // result = [...result, ...flattenArray(item)];
    } else {
      result.push(item);
    }
  });
  return result;
}

const arr = [1, 2, [3, 4, [5, 6]], 7];
const flattenedArr = flattenArray(arr);
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution 4:&lt;/strong&gt; using &lt;code&gt;recursion&lt;/code&gt; and &lt;code&gt;reduce method&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function flattenArray(arr) {
  return arr.reduce((acc, val) =&amp;gt; acc.concat(Array.isArray(val) ? flattenArray(val) : val), []);
}

const arr = [1, 2, [3, 4, [5, 6]], 7];
const flattenedArr = flattenArray(arr);
console.log(flattenedArr); // Output: [1, 2, 3, 4, 5, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌📌 More Javascript Tricky questions here:&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/tricky-javascript-code-4c0c"&gt;Tricky Javascript code part 1&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/md_ashraf_dev_to/handling-device-orientation-in-javascript-4aog"&gt;Tricky Javascript code - Handling device orientation &lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Accessibility in Web development &amp; Angular</title>
      <dc:creator>MD ASHRAF</dc:creator>
      <pubDate>Fri, 27 Jun 2025 14:30:00 +0000</pubDate>
      <link>https://dev.to/md_ashraf_dev_to/accessibility-in-web-development-angular-4kda</link>
      <guid>https://dev.to/md_ashraf_dev_to/accessibility-in-web-development-angular-4kda</guid>
      <description>&lt;h1&gt;
  
  
  What is Accessibility and why? 😡😡#
&lt;/h1&gt;

&lt;p&gt;It ensures equal access of information to people with &lt;code&gt;disabilities, promotes inclusivity, and improves overall user experience, while also enhancing reputation and compliance with laws and regulations.&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Accessible websites benefit people with disabilities, older adults, and everyone, regardless of abilities or environment. By prioritizing accessibility, organizations can create more inclusive and usable websites that reach a wider audience and drive better conversion rates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ways to achieve that in our website&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Use semantic HTML:&lt;/strong&gt;&lt;br&gt;
    - Use HTML elements that provide meaning to the structure of a web page, such as &lt;code&gt;header, nav, main, section, article, aside, footer,&lt;/code&gt; etc.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: `&amp;lt;header&amp;gt;...&amp;lt;/header&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;2.&lt;strong&gt;Provide alternative text for images:&lt;/strong&gt;&lt;br&gt;
    - Use the &lt;code&gt;alt attribute&lt;/code&gt; to provide a text description of images, which can be read by screen readers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: `&amp;lt;img src="image.jpg" alt="Image description"&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;3.&lt;strong&gt;Use ARIA attributes:&lt;/strong&gt;&lt;br&gt;
    - Use &lt;code&gt;ARIA attributes&lt;/code&gt; to provide additional information about dynamic content, such as &lt;code&gt;aria-label, aria-describedby, aria-expanded&lt;/code&gt;, etc. &lt;br&gt;
It can be read by screen readers so would be helpful for special group of people.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: `&amp;lt;button aria-label="Close"&amp;gt;X&amp;lt;/button&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;4.&lt;strong&gt;Ensure keyboard navigation:&lt;/strong&gt;&lt;br&gt;
    - Ensure that all interactive elements can be accessed using a keyboard, and that the tab order is logical.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: Use tabindex attribute to specify the tab order: &amp;lt;button tabindex="1"&amp;gt;...&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;5.&lt;strong&gt;Use high contrast colors:&lt;/strong&gt;&lt;br&gt;
    - Ensure that the color scheme has sufficient contrast between background and foreground colors, making it easier for users with visual impairments to read.&lt;br&gt;
    - Example: Use a tool like Snook's Color Contrast Checker to ensure sufficient contrast.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2os9nszm5yinqx5r11j5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2os9nszm5yinqx5r11j5.png" alt="Image contrast" width="795" height="672"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6.&lt;strong&gt;Provide closed captions for audio and video content:&lt;/strong&gt;&lt;br&gt;
    - Provide closed captions for audio and video content, which can be read by users who are not able to listen or hard of hearing.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: `&amp;lt;video&amp;gt; &amp;lt;track kind="captions" src="captions.vtt" srclang="en"&amp;gt; &amp;lt;/video&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;7.&lt;strong&gt;Use Angular's built-in accessibility features:&lt;/strong&gt;&lt;br&gt;
    - Use Angular's built-in accessibility features, such as the &lt;code&gt;AccessibilityModule&lt;/code&gt;, to improve the accessibility of your application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Example: `import { AccessibilityModule } from '@angular/cdk/a11y';`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;// &lt;em&gt;Here's a full code example of using Angular's built-in accessibility features:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;app.module.ts&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa5nm2vvtrjxhzbac34ie.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa5nm2vvtrjxhzbac34ie.png" alt="Image module" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;app.component.html&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gd2im0uhfjoy4mrqw53.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gd2im0uhfjoy4mrqw53.png" alt="Image component" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we're using Angular's built-in accessibility features by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Importing the &lt;code&gt;A11yModule from @angular/cdk/a11y&lt;/code&gt; in our module.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;ARIA attributes (aria-expanded, aria-label, aria-labelledby, aria-live, aria-atomic, aria-describedby)&lt;/code&gt; to provide additional information about dynamic content and interactive elements.&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;role attribute&lt;/code&gt;to define the role of elements (e.g., &lt;code&gt;menu, menuitem, tooltip&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>a11y</category>
      <category>web</category>
      <category>html</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
