<?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 Ram on Medium]]></title>
        <description><![CDATA[Stories by Ram on Medium]]></description>
        <link>https://medium.com/@bvn.ramkumar?source=rss-a437fb268169------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NKgOW_UgEV7WVi_1cdb_MA.jpeg</url>
            <title>Stories by Ram on Medium</title>
            <link>https://medium.com/@bvn.ramkumar?source=rss-a437fb268169------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 20:58:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bvn.ramkumar/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[Understanding the new Keyword in JavaScript — A Tea Shop Analogy]]></title>
            <link>https://medium.com/@bvn.ramkumar/understanding-the-new-keyword-in-javascript-a-tea-shop-analogy-9e4f45faa984?source=rss-a437fb268169------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e4f45faa984</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[jslearning]]></category>
            <category><![CDATA[javascript-new-keyword]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ram]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 12:44:41 GMT</pubDate>
            <atom:updated>2025-06-27T13:11:50.479Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6mpPFeNr48fFu0DKwppg7g.jpeg" /></figure><p><strong>Understanding the new Keyword in JavaScript — A Tea Shop Analogy</strong></p><p>If you are ever wondered what the new keyword does in javascript, let’s skip the jargon and step into a place we all know — a local tea shop.</p><p>Yes, we’re about to understand JavaScript by <strong>brewing tea</strong>. 🍵</p><p><strong>🧠 What Is the new Keyword?</strong></p><p>In JavaScript, the new keyword is used to create a <strong>new instance of an object</strong> using a <strong>constructor function</strong>.</p><p>Think of it as asking a machine to make something for you, like a fresh cup of tea.</p><p><strong>🫖 Welcome to the JavaScript Tea Shop</strong></p><p>Imagine you’re running a tea shop. You’ve got a machine that makes different types of tea based on customer preferences.</p><p>Let’s create that machine in code:</p><pre>function Tea(type, sugarLevel){<br>  this.type = type;<br>  this.sugarLevel = sugarLevel;<br>}</pre><p>This function is like your tea making machine, when you pass in the type of tea and sugar level , it will prepares a customized tea for your customer.</p><p>Now, you use new whenever a customer orders:</p><pre>const ramTea = new Tea(&quot;Ginger&quot;, &quot;Less Sugar&quot;);<br>const sitaTea = new Tea(&quot;Cardamom&quot;, &quot;No Sugar&quot;);</pre><p><strong>What Really Happens When new Tea() is Called:</strong></p><p>1. A <strong>new empty cup (object)</strong> is created</p><p>2. The <strong>tea machine (Tea function)</strong> prepares the tea and fills the cup</p><p>3. You get your <strong>custom cup of tea</strong> as a result</p><pre>console.log(ramTea);<br>// Output: { type: &quot;Ginger&quot;, sugarLevel: &quot;Less Sugar&quot; }<br><br>console.log(sitaTea);<br>// Output: { type: &quot;Cardamom&quot;, sugarLevel: &quot;No Sugar&quot; }</pre><p><strong>What if You Forget new?</strong></p><pre>const wrongTea = Tea(&quot;Masala&quot;, &quot;More Sugar&quot;);<br>console.log(wrongTea); // ❌ undefined</pre><p>Why? Because you didn’t use the tea machine correctly, so the tea wasn’t returned to your hands.</p><p><strong>Real-Life Example — A Tea Seller</strong></p><p>Let’s take it one step further:</p><pre>function Person(name, work) {<br>  this.name = name;<br>  this.work = work;<br>}<br><br>const teaSeller = new Person(&quot;Ramu&quot;, &quot;Tea Shop Owner&quot;);<br><br>console.log(teaSeller);</pre><p>Output:</p><pre>{ name: &quot;Ramu&quot;, work: &quot;Tea Shop Owner&quot; }</pre><p><strong>🧵 Summary</strong></p><p>• function Tea(…) is the tea-making machine.</p><p>• new Tea(…) creates a new customized cup of tea.</p><p>• Each call with new gives you a <strong>fresh object</strong>.</p><ul><li>Forgetting new means your tea isn’t made properly!</li></ul><p><strong>Final Words</strong></p><p>If you’re just starting with JavaScript and object-oriented programming feels intimidating — remember the tea shop.</p><p>Every new object is just another customer’s cup.</p><p>Would you like a follow-up on class, this, or prototype explained the same way? Let me know in the comments.</p><p><strong>🖊️ Written by:</strong> Ram</p><p><em>JavaScript Learner | Tea Lover | Medium Explorer</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e4f45faa984" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Reducer hook]]></title>
            <link>https://medium.com/@bvn.ramkumar/use-reducer-hook-8f7a56068829?source=rss-a437fb268169------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f7a56068829</guid>
            <category><![CDATA[counter]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[usereducer]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[usereducer-vs-usestate]]></category>
            <dc:creator><![CDATA[Ram]]></dc:creator>
            <pubDate>Mon, 09 Jun 2025 16:03:05 GMT</pubDate>
            <atom:updated>2025-06-09T16:03:05.209Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TBwUQ1BGVfzhk9mLJ9IAPw.png" /><figcaption>Use Reducer hook</figcaption></figure><p>React provides several built-in hooks to help manage state in functional components.</p><p>Among them, useState is the go-to for simple states. But when your state logic becomes more complex — like involving multiple values or depending on previous state — useReducer can be a much better choice.</p><p>Although it’s powerful, useReducer is often underused by beginners because it <em>sounds</em> more complicated than it actually is.</p><p>In this article, I’ll walk you through a <strong>simple and beginner-friendly use case</strong> of useReducer — a classic <strong>counter example</strong>. By the end, you’ll understand how it works and when to use it.</p><p>useState is like a quick notepad. you write something your state and update directly</p><pre>const [count, setCount] = useState(0);<br>setCount(count + 1); // update the state</pre><p>useState is suitable for simple functionalities, and easy to use. but it become messy with complex updates</p><p>useReducer is more like a decision-maker. You <strong>describe what you want</strong> (an action), and a function (called a reducer) decides <strong>how to update</strong> the state.</p><p>Let’s compare useState and useReducer using a basic counter example.</p><pre>import React, { useState } from &#39;react&#39;;<br><br>function Counter() {<br>  const [count, setCount] = useState(0);<br><br>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Count: {count}&lt;/h2&gt;<br>      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;+&lt;/button&gt;<br>      &lt;button onClick={() =&gt; setCount(count - 1)}&gt;-&lt;/button&gt;<br>      &lt;button onClick={() =&gt; setCount(0)}&gt;Reset&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>above the code is simple. But what if we had more actions ? the code become messy.</p><p>Here we see how to useReducer for above counter</p><p><strong>1. Import Dependencies</strong></p><p>first import dependencies from react library</p><pre>import React, { useReducer } from &#39;react&#39;;<br><br></pre><p><strong>2. Define the Initial State</strong></p><p>then define initial state</p><pre>const initialState = { count: 0 };</pre><p>the initial state is your starting state, it’s an object with a single property count set to 0,</p><p>Why an object instead of just a number? Because using an object allows you to easily <strong>add more state values later</strong> without rewriting everything.</p><p><strong>3. Create the Reducer Function</strong></p><pre>function reducer(state, action) {<br>  switch (action.type) {<br>    case &#39;increment&#39;:<br>      return { count: state.count + 1 };<br>    case &#39;decrement&#39;:<br>      return { count: state.count - 1 };<br>    case &#39;reset&#39;:<br>      return { count: 0 };<br>    default:<br>      return state;<br>  }<br>}</pre><p>this function takes two arguments, first one is the current state, second is an action, based on the action.type, it returns a new state.</p><p>in increment case increase count by 1, decrement state decrease it by 1 then reset sets it back to 0, default returns the existing state if the action is unknown (a safety fallback).</p><p><strong>4. Build the Component</strong></p><pre>function Counter() {<br>  const [state, dispatch] = useReducer(reducer, initialState);</pre><p>Here you call useReducer with reducer function and initial state value and it returns current state initially in state, and dispatch is a the function used to send actions that trigger the state updation.</p><p><strong>5. Render the UI</strong></p><pre>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Count: {state.count}&lt;/h2&gt;<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;increment&#39; })}&gt;+&lt;/button&gt;<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;decrement&#39; })}&gt;-&lt;/button&gt;<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;reset&#39; })}&gt;Reset&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>Here displaying the count using {state.count}, then three buttons provide different actions by using dispatch to reducer, when you click a button, the reducer gets called with corresponding action, it update the state then react re-renders the component with the new state.</p><p><strong>6. Export the Component</strong></p><pre>export default Counter;</pre><p>it allows you to use the counter component elsewhere in your app.</p><p><strong>Simple Counter Example using useReducer</strong></p><pre>import React, { useReducer } from &#39;react&#39;;<br><br>// 1️⃣ Step 1: Define the initial state<br>const initialState = { count: 0 };<br><br>// 2️⃣ Step 2: Create the reducer function<br>function reducer(state, action) {<br>  switch (action.type) {<br>    case &#39;increment&#39;:<br>      return { count: state.count + 1 };<br>    case &#39;decrement&#39;:<br>      return { count: state.count - 1 };<br>    case &#39;reset&#39;:<br>      return { count: 0 };<br>    default:<br>      return state;<br>  }<br>}<br><br>// 3️⃣ Step 3: Create the Counter component<br>function Counter() {<br>  // useReducer gives us the current state and a dispatch function<br>  const [state, dispatch] = useReducer(reducer, initialState);<br><br>  return (<br>    &lt;div style={{ textAlign: &#39;center&#39;, marginTop: &#39;50px&#39; }}&gt;<br>      &lt;h2&gt;Count: {state.count}&lt;/h2&gt;<br><br>      {/* Dispatch actions to update the state */}<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;increment&#39; })}&gt;+&lt;/button&gt;<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;decrement&#39; })}&gt;-&lt;/button&gt;<br>      &lt;button onClick={() =&gt; dispatch({ type: &#39;reset&#39; })}&gt;Reset&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default Counter;</pre><p>This example is a <strong>great starting point</strong> for learning useReducer. From here, you can expand it to manage more complex data like form inputs, to-do lists, or UI state.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f7a56068829" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>