<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vandana Rao Cherukuru on Medium]]></title>
        <description><![CDATA[Stories by Vandana Rao Cherukuru on Medium]]></description>
        <link>https://medium.com/@vandanacherukuru?source=rss-9f3eb2a165ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7-cVTq2jtRoWpsQqXLUJMw.jpeg</url>
            <title>Stories by Vandana Rao Cherukuru on Medium</title>
            <link>https://medium.com/@vandanacherukuru?source=rss-9f3eb2a165ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 16:33:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vandanacherukuru/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular : Passing data between routes]]></title>
            <link>https://medium.com/@vandanacherukuru/angular-passing-data-between-routes-5ee0e26a5c34?source=rss-9f3eb2a165ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ee0e26a5c34</guid>
            <category><![CDATA[passing-data]]></category>
            <category><![CDATA[routing]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Vandana Rao Cherukuru]]></dc:creator>
            <pubDate>Thu, 03 Apr 2025 07:45:11 GMT</pubDate>
            <atom:updated>2025-04-03T07:45:11.298Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular : Passing data between routes</h3><ol><li>Using Route Parameters =&gt; for simple id’s</li><li>Using Query Parameters =&gt; Optional filtering</li><li>Using Route Data =&gt; static route specific data =&gt; ex: title</li><li>Using Services (Shared Service) =&gt; to share complex data</li><li>Using state in router.navigate() (Navigation Extras) =&gt; temporary data for the immediately navigated-to component</li></ol><p><strong>1. Using Route Parameters:</strong></p><ul><li><strong>How it works:</strong> You define parameters directly in your route configuration. When navigating, you include the data as part of the URL. The receiving component can then extract this data from the ActivatedRoute service.</li><li><strong>Best for:</strong> Passing simple, identifiable data like IDs or names that are part of the resource being displayed.</li></ul><pre>// app-routing.module.ts<br>const routes: Routes = [<br>  { path: &#39;products/:id&#39;, component: ProductDetailsComponent }<br>];<br><br>// product-list.component.ts<br>this.router.navigate([&#39;/products&#39;, product.id]);<br><br>// product-details.component.ts<br>constructor(private route: ActivatedRoute) {<br>  this.route.paramMap.subscribe(params =&gt; {<br>    const productId = params.get(&#39;id&#39;);<br>  });<br>//const id = this.route.snapshot.paramMap.get(&#39;id&#39;);<br>}</pre><p>2. <strong>Using Query Parameters:</strong></p><ul><li><strong>How it works:</strong> You append data to the URL after a question mark (?) as key-value pairs. The receiving component can access these parameters using the ActivatedRoute service.</li><li><strong>Best for:</strong> Passing optional data, filtering criteria, or search terms that don’t fundamentally change the route.</li></ul><pre>// product-list.component.ts<br>this.router.navigate([&#39;/products&#39;], { queryParams: { category: &#39;electronics&#39;, sortBy: &#39;price&#39; } });<br>//url -- products?category=electronics&amp;sortBy=price<br><br>// product-details.component.ts<br>constructor(private route: ActivatedRoute) {<br>  this.route.queryParamMap.subscribe(queryParams =&gt; {<br>    const category = queryParams.get(&#39;category&#39;);<br>    const sortBy = queryParams.get(&#39;sortBy&#39;);<br>  });<br>}</pre><p><strong>3. Using Route Data:</strong></p><ul><li><strong>How it works:</strong> You define static data directly within your route configuration using the data property. This data is available to the activated route.</li><li><strong>Best for:</strong> Passing static information related to the route itself, such as page titles, breadcrumb labels, or authentication roles.</li></ul><pre>// app-routing.module.ts<br>const routes: Routes = [<br>  { path: &#39;profile&#39;, component: ProfileComponent, data: { title: &#39;User Profile&#39; } }<br>];<br><br>// profile.component.ts<br>constructor(private route: ActivatedRoute, private titleService: Title) {<br>  this.route.data.subscribe(data =&gt; {<br>    this.titleService.setTitle(data[&#39;title&#39;]);<br>  });<br>}</pre><p><strong>4. Using </strong><strong>state in </strong><strong>router.navigate() (Navigation Extras):</strong></p><ul><li><strong>How it works:</strong> You can pass data as part of the NavigationExtras object when using router.navigate(). This data is available in the history.state of the browser.</li><li><strong>Best for:</strong> Passing temporary data that shouldn’t be reflected in the URL and is only needed by the immediately navigated-to component. This data is lost on a hard refresh.</li></ul><pre>// component-a.component.ts<br>this.router.navigate([&#39;/component-b&#39;], { state: { user: { id: 123, name: &#39;John Doe&#39; } } });<br><br>// component-b.component.ts<br>constructor(private router: Router) {<br>  this.user = this.router.getCurrentNavigation()?.extras?.state?.[&#39;user&#39;];<br>  if (this.user) {<br>    console.log(this.user.name); // Output: John Doe<br>  }<br>}</pre><p><strong>Which Method to Use?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mngASjyTb2-guUijcX7NXQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ee0e26a5c34" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spread vs Rest Operator in JS]]></title>
            <link>https://medium.com/@vandanacherukuru/spread-vs-rest-operator-in-js-4f8b240daaf1?source=rss-9f3eb2a165ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f8b240daaf1</guid>
            <category><![CDATA[rest-operator]]></category>
            <category><![CDATA[spread-operator]]></category>
            <category><![CDATA[rest-vs-spread]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Vandana Rao Cherukuru]]></dc:creator>
            <pubDate>Wed, 02 Apr 2025 16:47:52 GMT</pubDate>
            <atom:updated>2025-04-08T12:00:01.133Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Spread Operator:</strong></p><p>The spread operator expands an <strong>iterable</strong> (like an array, string, Map, or Set) into its individual elements. When used with an <strong>object</strong> within an object literal, it copies the object’s key-value pairs. To achieve array-like spreading of an object’s values, the object needs to be explicitly made iterable using Symbol.iterator. The rest operator, on the other hand, collects multiple elements into a single array or object. They both use the same syntax (three dots: ...), but their purpose is opposite.</p><p><strong>Purpose:</strong> To unpack or insert elements from an iterable (array, string, Map, Set) or properties from an object into a new array or object literal, respectively.</p><p><strong>Use Cases:</strong></p><ul><li>Copying arrays or objects.</li><li>Merging arrays or objects.</li><li>Passing arguments to a function.</li><li>Creating new arrays or objects with additional elements or properties.</li></ul><pre> //spread in array literal(literal means fixed value)<br>    const arr1 = [1, 2, 3];<br>    const arr2 = [4, 5, 6];<br>    const combinedArr = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6] - Merge array<br>    const copiedArr = [...arr1]; // [1, 2, 3] <br>    console.log(combinedArr,copiedArr)<br><br>    //spread in Object literal<br>    const obj1 = { a: 1, b: 2 };<br>    const obj2 = { b: 3, c: 4 };<br>    const mergedObj = { ...obj1, ...obj2 }; <br>    console.log(mergedObj);// { a: 1, b: 3, c: 4 }<br><br>    //spread in function call<br>    function myFunction(a, b, c) {<br>      console.log(a, b, c);<br>    }<br>    const arr = [1, 2, 3];<br>    myFunction(...arr); // Output: 1 2 3</pre><p><strong>Rest Operator:</strong></p><p><strong>Purpose:</strong> Collects multiple arguments or elements and condenses them into a single array or object.</p><p><strong>Use Cases: (</strong>function parameters and destructuring assignments)</p><ul><li>Defining functions with a variable number of arguments.</li><li>Destructuring arrays or objects to capture remaining elements or properties.</li></ul><pre> //rest in Array Destructuring<br>    const [x,y,...z] = [1, 2, 3,4,5];<br>    console.log(&quot;x:&quot;,x,&quot;y:&quot;,y,&quot;z:&quot;,z) //&quot;x:&quot;,  1,  &quot;y:&quot;,  2,  &quot;z:&quot;,  [3, 4, 5] <br><br>  //rest in Object Destructuring<br>  const {a,b,...r} = { a: 1, b: 2 ,c: 4, d:3};<br>  console.log(&quot;a:&quot;,a,&quot;b:&quot;,b,&quot;r:&quot;,r)  //&quot;a:&quot;,  1,  &quot;b:&quot;,  2,  &quot;r:&quot;,  { &quot;c&quot;: 4,&quot;d&quot;: 3} <br>  <br>  //rest in function parameters<br>  function myFunction(a, ..rest) {<br>    console.log(&quot;a:&quot;,a, &quot;rest:&quot;,rest); //&quot;a:&quot;,  1,  &quot;rest:&quot;,  [2, 3]<br>  }<br>  const arr = [1, 2, 3];<br>  myFunction(...arr);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f8b240daaf1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Internationalization (Intl.DateTimeFormat)]]></title>
            <link>https://medium.com/@vandanacherukuru/javascript-internalization-intl-datetimeformat-e8c0bb91a904?source=rss-9f3eb2a165ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8c0bb91a904</guid>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[datetimeformatter]]></category>
            <category><![CDATA[internalization]]></category>
            <dc:creator><![CDATA[Vandana Rao Cherukuru]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 17:22:33 GMT</pubDate>
            <atom:updated>2025-04-01T17:35:49.448Z</atom:updated>
            <content:encoded><![CDATA[<p>The Intl.DateTimeFormat object in JavaScript is used to <strong>format dates and times in a locale-sensitive manner</strong>. It allows you to control how dates and times are presented to users based on their language, region, and preferences.</p><pre>import { Component} from &#39;@angular/core&#39;;<br><br>@Component({<br>  selector: &#39;my-app&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>})<br>export class AppComponent {<br><br>  // Option 1: Get timezone from the browser (less reliable)<br>  // private userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;<br><br>  // Option 2: Store user timezone (e.g., fetched from server or user preference)<br><br>  private userTimeZone: string | null = null;<br><br>  ngOnInit() {<br>    const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;<br>    console.log(&#39;User Timezone (Browser):&#39;, userTimeZone);<br>    this.setUserTimeZone(userTimeZone);<br>    this.logTime();<br>  }<br>  setUserTimeZone(timezone: string) {<br>    this.userTimeZone = timezone;<br>  }<br><br>  logTime() {<br>    const now = new Date();<br>    const options: Intl.DateTimeFormatOptions = {<br>      year: &#39;numeric&#39;,<br>      month: &#39;long&#39;,<br>      day: &#39;numeric&#39;,<br>      hour: &#39;2-digit&#39;,<br>      minute: &#39;2-digit&#39;,<br>      second: &#39;2-digit&#39;,<br>      timeZoneName: &#39;short&#39;,<br>    };<br><br>    let formattedTime: string;<br><br>    if (this.userTimeZone) {<br>      options.timeZone = this.userTimeZone;<br>      formattedTime = now.toLocaleString(&#39;en-US&#39;, options);<br>    } else {<br>      // Fallback to the browser&#39;s local timezone if userTimeZone is not set<br>      formattedTime = now.toLocaleString(&#39;en-US&#39;, options);<br>    }<br><br>    console.log(`[${formattedTime}]` );<br>    console.log(&quot;Sys time&quot;,now.toLocaleString())<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*iAZAcckfOIvgNh2GK24Mgw.png" /><figcaption>Output</figcaption></figure><p><strong>Key Use Cases and Benefits of </strong><strong>Intl.DateTimeFormat:</strong></p><ul><li><strong>Displaying Dates and Times to Users:</strong> This is the most common use case. You want to show dates and times in a format that is familiar and natural to the user based on their location or language. For example, in the US, you might display dates as “MM/DD/YYYY”, while in Europe, it might be “DD/MM/YYYY”.</li><li><strong>Internationalization (i18n) and Localization (l10n):</strong> It’s a fundamental part of making web applications accessible to a global audience. Intl.DateTimeFormat handles the complexities of different date and time formats, timezones, and calendar systems. often intricate and can vary significantly between regions.</li><li><strong>Handling Timezones:</strong> It allows you to format dates and times according to specific timezones, which is crucial when dealing with users in different geographical locations.</li></ul><p>The key difference between new Date() and Intl.DateTimeFormat() lies in <strong>what they represent and what they are used for</strong>:</p><p><strong>new Date():</strong></p><ul><li><strong>Represents a specific point in time.</strong> When you call new Date(), it creates a JavaScript object that holds a timestamp, typically representing the current date and time in the user&#39;s <strong>local system&#39;s timezone</strong>.</li><li><strong>Provides methods to access and manipulate date and time components.</strong> You can use methods like getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(), getTime(), toISOString(), etc., to get individual parts of the date and time or to convert it to different formats.</li><li><strong>Default output is often not user-friendly for different locales.</strong> When you directly convert a Date object to a string using toString() or toLocaleString() without specifying a locale, the format will be based on the browser&#39;s or system&#39;s default locale. This might not be suitable for users in other regions.</li><li><strong>Focuses on the underlying time value.</strong> Its primary purpose is to store and work with the raw time information.</li></ul><p><strong>Intl.DateTimeFormat():</strong></p><ul><li><strong>Is a formatter for displaying dates and times in a locale-sensitive way.</strong> It doesn’t represent a specific point in time itself. Instead, it’s an object that knows how to take a Date object and format it into a string according to the conventions of a particular language and region.</li><li><strong>Takes a </strong><strong>Date object as input to its </strong><strong>format() method.</strong> You need a Date object to use with Intl.DateTimeFormat().</li><li><strong>Provides highly customizable output based on locale and options.</strong> You can specify the locale (e.g., ‘en-US’, ‘fr-FR’, ‘de-DE’) and various options (like dateStyle, timeStyle, timeZone, year, month, day, hour, minute, etc.) to control the format of the output string.</li><li><strong>Focuses on presentation and internationalization.</strong> Its primary purpose is to present date and time information in a way that is understandable and natural for users from different cultural backgrounds.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8c0bb91a904" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hoisting (variable and function) and Temporal Dead Zone in JavaScript]]></title>
            <link>https://medium.com/@vandanacherukuru/hoisting-variable-and-function-and-temporal-dead-zone-in-javascript-0a7150087ba2?source=rss-9f3eb2a165ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a7150087ba2</guid>
            <category><![CDATA[hoisting]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[function-hoisting]]></category>
            <category><![CDATA[variable-hoisting]]></category>
            <category><![CDATA[temporal-dead-zone]]></category>
            <dc:creator><![CDATA[Vandana Rao Cherukuru]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 12:06:31 GMT</pubDate>
            <atom:updated>2025-04-01T12:15:54.726Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hoisting(variable and function), Temporal Dead Zone in JavaScript</h3><p>Hoisting is a JavaScript mechanism where <strong>variable and function declarations are moved (“hoisted”) to the top of their scope</strong> before code execution. This means you can use variables(type Var) and functions before declaring them in the code.</p><ul><li><strong>Declarations are Hoisted, Not Initializations:</strong> Only the declaration part is hoisted. Assignments or initializations are not moved.</li><li><strong>Scope Matters:</strong> Hoisting happens within the scope of the variable or function (global or function scope).</li></ul><ol><li><strong>Variable Hoisting :</strong></li></ol><ul><li><strong>var is Hoisted, </strong><strong>let and </strong><strong>const are Not (Exactly):</strong> While let and const declarations are also technically &quot;hoisted,&quot; they are not initialized, leading to a ReferenceError</li><li>if you try to access them before their actual declaration in the code it will throw ReferenceError. This is often referred to as the &quot;temporal dead zone&quot; (TDZ).</li></ul><pre>console.log(&quot;x:&quot;,x) // Output: x: undefined<br>// if tried to access y and z in above line it will throw - ReferenceError: Cannot access &#39;y&#39; before initialization<br>var x = 10<br>let y= 5<br>const z=8<br>console.log(&quot;x:&quot;,x,&quot;y:&quot;,y,&quot;z:&quot;,z) // Output: x: 10 y: 5 z:     </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ai6cu7Sie9mIGjq6w2UImg.png" /></figure><h4>2. Function Hoisting:</h4><p>Function Declaration Hoisting:</p><pre>welcome(&quot;vandana&quot;); // Output: Hello, vandana!<br><br>function welcome(name) {<br>  console.log(&quot;Hello, &quot; + name + &quot;!&quot;); <br>}</pre><p>Function Expression Hoisting:</p><p>Only the variable declaration var welcome; is hoisted. The function assignment (name) =&gt; { ... } is not hoisted. So, when you try to call welcomebefore the assignment, it&#39;s still undefined, and you get a TypeError.</p><pre>welcome(&quot;vandana&quot;); // Output: TypeError: welcome is not a function<br><br>//function Expression <br>var welcome = (name)=&gt; {<br>  console.log(&quot;Hello, &quot; + name + &quot;!&quot;);<br>};<br><br>welcome(&quot;vandana&quot;); // Output: Hello, vandana!</pre><p>Why TypeError instead of undefined?</p><p>You get a TypeError instead of undefined when trying to call a function expression before its assignment, because of how JavaScript handles the hoisted variable</p><p>When you have a function expression like above:</p><p>During the compilation phase, only the variable declaration var welcome; is hoisted to the top of the scope. The function assignment (name) =&gt;{ ... } remains in its original position.</p><p><strong>What Happens When You Try to Call It Before Assignment:</strong></p><ol><li><strong>Hoisting:</strong> The JavaScript engine processes the code and moves the var welcome; declaration to the top. At this point, welcomeexists in the scope.</li><li><strong>Initial Value:</strong> Because it’s declared with var, welcomeis initialized with the value undefined.</li><li><strong>Attempting the Function Call:</strong> When the engine reaches the line welcome(&quot;vandana&quot;); <em>before</em> the actual function assignment, the value of welcomeis undefined.</li><li><strong>The </strong><strong>TypeError:</strong> JavaScript&#39;s type system (which is dynamic but still has rules) knows that undefined is not a function. You cannot execute undefined like a function. Therefore, it throws a TypeError indicating that you&#39;re trying to call something that isn&#39;t a function.</li></ol><p>In summary, the TypeError when calling a function expression before assignment is a direct consequence of the variable being hoisted but initialized to undefined. JavaScript correctly identifies that undefined is not callable and throws an error to prevent further issues and aid in debugging.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a7150087ba2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Subjects and it’s use cases]]></title>
            <link>https://medium.com/@vandanacherukuru/angular-subjects-and-its-use-cases-89304ca45f0a?source=rss-9f3eb2a165ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/89304ca45f0a</guid>
            <category><![CDATA[replaysubject]]></category>
            <category><![CDATA[subject]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[asyncsubject]]></category>
            <category><![CDATA[behaviorsubject]]></category>
            <dc:creator><![CDATA[Vandana Rao Cherukuru]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 10:18:50 GMT</pubDate>
            <atom:updated>2025-04-01T10:54:45.017Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Subject:</strong></h3><ul><li><strong>Subjects</strong> emit values to multiple subscribers but don’t store previous values.</li><li>New subscribers will <strong>not receive past values</strong>.</li><li>Only receives values <strong>after subscribing</strong>.</li></ul><p>Use Case :</p><p>✅ When <strong>no past values</strong> need to be stored.<br>✅ Ideal for <strong>event-based</strong> systems (e.g., button clicks, user actions) and Broadcasting UI events across components (Suppose you have multiple components that need to listen to a global event, like a <strong>user clicking a logout button</strong>.)</p><pre>import { Component } from &#39;@angular/core&#39;;<br>import { Subject } from &#39;rxjs&#39;;<br><br>@Component({<br>  selector: &#39;my-app&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>})<br>export class AppComponent {<br>  subject = new Subject&lt;number&gt;();<br><br>  ngOnInit() {<br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 1:&#39;, value));<br><br>    this.subject.next(1);<br>    this.subject.next(2);<br><br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 2:&#39;, value));<br><br>    this.subject.next(3);<br>  }<br>}</pre><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*CDmyLWDd6BSkjCuerVrk_g.png" /><figcaption>Output</figcaption></figure><h3><strong>BehaviorSubject:</strong></h3><ul><li><strong>BehaviorSubject</strong> store the last value and emit it to new subscribers immediately</li><li>Ensures that subscribers always <strong>get the most recent data</strong>.</li><li>Requires an <strong>initial value</strong>.</li></ul><p>Use Case: <strong>Storing and sharing state across multiple components</strong></p><pre>import { Component} from &#39;@angular/core&#39;;<br>import { BehaviorSubject, Subject } from &#39;rxjs&#39;;<br><br>@Component({<br>  selector: &#39;my-app&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>})<br>export class AppComponent {<br><br>  subject = new BehaviorSubject&lt;number&gt;(1);<br>  ngOnInit() {<br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 1:&#39;, value));<br><br>    this.subject.next(1);<br>    this.subject.next(2);<br><br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 2:&#39;, value));<br><br>    this.subject.next(3);<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/183/1*SocGcfHG2eK2wJWHuNMXBg.png" /><figcaption>Output: Behavior Subject</figcaption></figure><h3><strong>ReplaySubject:</strong></h3><ul><li>Stores a <strong>history of emitted values</strong> and replays them to new subscribers.</li><li>The number of values to store is configurable.</li></ul><p>Use case:</p><p>✅ When you need to <strong>cache multiple past values</strong> (e.g., logging events).<br>✅ Ideal for <strong>chat history, analytics, or buffering API responses</strong>.</p><p>The key difference between ReplaySubject (with a buffer) and BehaviorSubject lies in <strong>what they emit to new subscribers</strong>:</p><ul><li><strong>BehaviorSubject</strong>: Emits the <strong>most recent value</strong> emitted by the Observable to new subscribers. It also requires an initial value upon creation.</li><li><strong>ReplaySubject (with a buffer)</strong>: Emits a <strong>specified number of past values</strong> (defined by the buffer size) to new subscribers. It doesn&#39;t require an initial value.</li></ul><p><strong>In essence:</strong></p><ul><li>Use BehaviorSubject when you need new subscribers to get the current state of something.</li><li>Use ReplaySubject when you need new subscribers to catch up on recent events or history.</li></ul><p><strong>Think of it like this:</strong></p><ul><li><strong>BehaviorSubject</strong>: Like asking someone &quot;What&#39;s the current time?&quot; They tell you the time <em>right now</em>.</li><li><strong>ReplaySubject</strong>: Like asking someone &quot;What were the last two things said?&quot; They tell you the previous two statements.</li></ul><pre>import { Component } from &#39;@angular/core&#39;;<br>import { BehaviorSubject, ReplaySubject, Subject } from &#39;rxjs&#39;;<br><br>@Component({<br>  selector: &#39;my-app&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>})<br>export class AppComponent {<br><br>  subject = new ReplaySubject&lt;number&gt;(2);<br>  ngOnInit() {<br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 1:&#39;, value));<br><br>    this.subject.next(1);<br>    this.subject.next(2);<br><br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 2:&#39;, value));<br><br>    this.subject.next(3);<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*ht_OpwsqlFBiE3JLh3fHnQ.png" /><figcaption>ReplaySubject Output</figcaption></figure><p><strong>Explanation:</strong></p><ol><li><strong>subject = new ReplaySubject&lt;number&gt;(2);</strong>: A ReplaySubject is initialized with a buffer size of 2. This means the ReplaySubject will store the last two emitted values and replay them to any new subscriber.</li><li><strong>this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 1:&#39;, value));</strong>: The first subscriber subscribes to the subject. At this point, no values have been emitted, so Subscriber 1 doesn&#39;t receive anything immediately.</li><li><strong>this.subject.next(1);</strong>: The value 1 is emitted. The ReplaySubject stores this value in its buffer. Subscriber 1 receives 1.</li><li><strong>this.subject.next(2);</strong>: The value 2 is emitted. The ReplaySubject stores this value in its buffer, and the buffer now contains [1, 2]. Subscriber 1 receives 2.</li><li><strong>this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 2:&#39;, value));</strong>: The second subscriber subscribes to the subject. Because it&#39;s a ReplaySubject with a buffer size of 2, Subscriber 2 receives the last two emitted values, which are 1 and 2.</li><li><strong>this.subject.next(3);</strong>: The value 3 is emitted. The ReplaySubject adds this to its buffer (which might now be conceptually [2, 3] if it strictly keeps only the last two), and both Subscriber 1 and Subscriber 2 receive 3.</li></ol><h3><strong>AsyncSubject</strong>:</h3><ul><li>Only emits the <strong>last value</strong> when the subject <strong>completes</strong>.</li><li>Subscribers won’t receive any value until .complete() is called.</li></ul><p>UseCase:</p><p>✅ When you need <strong>only the final computed value</strong> (e.g., HTTP requests, final results).<br>✅ Useful in <strong>long-running operations</strong> where only the last value matters.</p><pre>import { Component, VERSION } from &#39;@angular/core&#39;;<br>import { AsyncSubject } from &#39;rxjs&#39;;<br><br>@Component({<br>  selector: &#39;my-app&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.css&#39;],<br>})<br>export class AppComponent {<br>  name = &#39;Angular &#39; + VERSION.major;<br><br>  subject = new AsyncSubject&lt;number&gt;();<br>  ngOnInit() {<br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 1:&#39;, value));<br><br>    this.subject.next(1);<br>    this.subject.next(2);<br><br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 2:&#39;, value));<br><br>    this.subject.next(4);<br>    this.subject.next(9);<br>    this.subject.complete();<br>    this.subject.subscribe((value) =&gt; console.log(&#39;Subscriber 3:&#39;, value));<br>    <br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*ejmRgQt8VlO8opw6ByJr-g.png" /><figcaption>AsyncSubject Output</figcaption></figure><h3>Summary:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VylY7F5T-xUlr7i4z0G46Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89304ca45f0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>