<?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 Sriram on Medium]]></title>
        <description><![CDATA[Stories by Sriram on Medium]]></description>
        <link>https://medium.com/@Sriram23?source=rss-1d2ca5d74008------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sp2J_xMCRSQF7MPj7-Z9-w.png</url>
            <title>Stories by Sriram on Medium</title>
            <link>https://medium.com/@Sriram23?source=rss-1d2ca5d74008------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 13:18:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Sriram23/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[How I Added Streaming to Sudeshi AI Chat]]></title>
            <link>https://medium.com/@Sriram23/how-i-added-streaming-to-sudeshi-ai-chat-02c63e6ffe3a?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/02c63e6ffe3a</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[side-hustle]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Fri, 03 Jul 2026 14:20:29 GMT</pubDate>
            <atom:updated>2026-07-03T14:20:29.430Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vzHBix-TDLZktLQGq-tYSw.png" /></figure><p>In the <a href="https://medium.com/@Sriram23/the-moment-sudeshi-became-more-than-a-ui-wrapper-c6a28bd4b297">previous article</a>, I wrote about how I discovered streaming while building Sudeshi. In this article, I’ll discuss in detail how I implemented the streaming response for the chat.</p><h3>My Initial Thoughts</h3><p>It sounded simple at first. Hit the API, get the response, render it on screen. But for a complex prompt, it took a while for the API to return the response. It was unlike the other AI chat applications like ChatGPT or Claude, which start responding immediately.</p><p>After reading through the Sarvam docs, I found they support streaming responses. Instead of waiting for the entire completion, the model could send smaller chunks as they were generated. Initially, I thought “Nice, it wouldn’t take much time”. I was wrong.</p><h3>The First Step</h3><p>Until now, the response flow looked something like this:</p><blockquote>Browser → Next.js API → Sarvam → Wait for response to complete → Render</blockquote><p>To support streaming, this entire flow had to change. Instead of waiting for one big response, I had to continuously read smaller chunks and keep updating the assistant message.</p><h3>Streaming…</h3><p>This was the first time I’ve worked with streaming APIs. Instead of response.json() I had to work with the response body as a stream. The browser exposed a ReadableStream, and I kept reading until the stream ended.</p><p>After enabling streaming on the API, the next step was continuously reading chunks from the response stream.</p><p>Seeing the first few chunks arriving in the browser was exciting. For the first time, the application felt alive. Instead of loading for a while, I could see the answer being built word by word. This small change completely changed the experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07UgGWKx8KLHihfFWhefFg.gif" /><figcaption>Streaming in Sudeshi Application</figcaption></figure><h3>The New Problem</h3><p>Receiving chunks was easy, but handling the state wasn’t. Initially, I considered every incoming new chunk as a new message. That obviously didn’t work.</p><p>The model wasn’t sending multiple messages. It was sending one message in multiple pieces. So, I had to change it like: instead of pushing new messages into the state, I had to append the last assistant message until the stream finished, then push it into the conversation state. This made the state updates of Sudeshi different from other chat applications.</p><p>This is the helper responsible for continuously reading chunks from the response stream.</p><pre>// Streaming the response<br>const reader = stream.getReader()<br>const decoder = new TextDecoder(&quot;utf-8&quot;);<br><br>let buffer = &quot;&quot;;<br><br>while(true) {<br>    const { done, value } = await reader.read();<br>    if (done) {<br>        console.log(&quot;Stream complete&quot;);<br>        break;<br>    }<br>    buffer += decoder.decode(value, { stream: true });<br><br>    const lines = buffer.split(&quot;\n&quot;);<br><br>    buffer = lines.pop() || &quot;&quot;;<br><br>    for (const line of lines) {<br>        const result = parser(line.trim())<br>        if(!result) continue<br><br>        if(result.text) {<br>            onChunk(result.text)<br>        }<br>    }<br>}</pre><h3>The Architecture Change</h3><p>Before streaming, the application flow was straightforward.</p><blockquote>Prompt → API → Model → Complete Response → Render</blockquote><p>After streaming, there were a few more moving pieces.</p><blockquote>Prompt → API → Model → Streaming Response → Process Chunk → Update Assistant message → Render → Repeat…</blockquote><p>The architecture wasn’t dramatically bigger, but Sudeshi was no longer a “call the API and display the response” application. Now, the frontend was actively participating while the model was still generating.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4uRwecw79DEUNpogtrlUPA.png" /><figcaption>Streaming Architecture Evolution of Sudeshi</figcaption></figure><h3>Looking back</h3><p>When I thought about streaming, I thought it was just a UI improvement. Looking back, it affected much more than that. It changed how responses were processed, how messages were stored, and how the frontend updated the state. And perhaps most importantly, it made Sudeshi feel responsive without making the model any faster.</p><p><strong>Streaming didn’t reduce the generation time. It reduced the waiting experience. </strong>That was one of the first moments when I realised perceived performance matters just as much as actual performance.</p><p>Streaming solved one problem but exposed another. Once responses started arriving continuously, I began noticing the UI itself becoming the bottleneck. That’s what I’ll write about next.</p><p>You can try Sudeshi here:</p><p>Live:</p><p><a href="https://sudeshi-ai-chat.vercel.app/">Sudeshi</a></p><p>Source Code:</p><p><a href="https://github.com/sriram23/sudeshi-ai-chat">GitHub - sriram23/sudeshi-ai-chat: Powered by Sarvam</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=02c63e6ffe3a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Moment Sudeshi Became More Than a UI Wrapper]]></title>
            <link>https://medium.com/@Sriram23/the-moment-sudeshi-became-more-than-a-ui-wrapper-c6a28bd4b297?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6a28bd4b297</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[side-hustle]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Thu, 25 Jun 2026 06:09:58 GMT</pubDate>
            <atom:updated>2026-07-04T12:56:58.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kSLc8kZBTkxMH_xSCTVA0w.png" /></figure><h3>The exploration phase</h3><p>After deciding to build something with AI, I needed a model to experiment with. Initially, I tried <a href="https://ollama.com/">Ollama</a>. Running AI models locally initially sounded interesting, but my machine wasn’t powerful enough to get a smoother experience. Also, I wasn’t sure what direction I wanted to take yet.</p><p>So, I started exploring hosted options. That’s when I came across <a href="https://www.sarvam.ai/">Sarvam</a> and started experimenting with their chat completion model, <a href="https://docs.sarvam.ai/api-reference-docs/getting-started/models/sarvam-30b">Sarvam 30B</a>. At that point, my plan was extremely simple. In my head, this was barely more than a frontend exercise.</p><p>Build a UI → Get user input → Hit the API → Get the response → Render in UI → Done.</p><h3>The First Version and what I learnt from it</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uTxQtV_pTPSngUURHTUA8w.png" /><figcaption>The first version of Sudeshi</figcaption></figure><p>I built the first version quickly. Technically, everything worked. There was a chatbox to collect user input, which then called the API and rendered the response in the UI. But the context was missing.</p><h3>The First Problem: Remembering Conversations</h3><p>At this point, the app wasn’t able to relate my first message and the subsequent messages. I felt this was incomplete and wanted to fix it. I started thinking about how to make this context-aware. The first thing that appeared to me was the state. When the conversation becomes longer than one interaction, I preserve it in a state and send it in the request to the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*kshp0LUO2y3bjLoiGf8y7g.png" /><figcaption>This is how the conversations are stored in the state</figcaption></figure><h3>Streaming Responses</h3><p>Simple prompts returned the response instantly, within a second. But larger prompts took noticeably longer. For example, if I ask something like: “Can you write a solution for the bracket validator problem in JavaScript?” I hit the send button, waited 5 seconds, and nothing happened. After some time, the response suddenly appeared. It’s technically correct, but it didn’t feel right.</p><p>That was the moment I started paying attention. When I use ChatGPT or Claude, responses used to start appearing almost immediately. Not the complete response, just enough to make the interaction feel alive. And this thought led me into “streaming”. I started learning how responses can be sent incrementally instead of waiting for the entire request to finish.</p><p>That led me into:</p><ul><li>Streaming</li><li>Server-Sent Events (SSE)</li><li>Chunks</li><li>Incremental Rendering</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UKaP1uR2LIJA5bNbuIQbzg.png" /><figcaption>A screenshot of Sudeshi streaming a response (taken on June 25th, 2026)</figcaption></figure><p>It sounded simple at first.</p><p>Receive data → Append → Render.</p><p>But that decision ended up changing more than I expected.</p><p>In the next article, I’ll write about my experience working with Streaming in Sudeshi.</p><p>If you’re curious to explore what I built, feel free to play around with Sudeshi!</p><p>Live Application</p><p><a href="https://sudeshi-ai-chat.vercel.app/">Sudeshi</a></p><p>Source Code</p><p><a href="https://github.com/sriram23/sudeshi-ai-chat">GitHub - sriram23/sudeshi-ai-chat: Powered by Sarvam</a></p><p>If you explore it and have suggestions, I’d love to hear them.</p><p>If you’re wondering why I built Sudeshi, read this story:</p><p><a href="https://medium.com/@Sriram23/why-i-started-building-sudeshi-ai-chat-cf23033bf74f">Why I Started Building Sudeshi AI Chat</a></p><p>More related stories:</p><p><a href="https://medium.com/@Sriram23/how-i-added-streaming-to-sudeshi-ai-chat-02c63e6ffe3a">How I Added Streaming to Sudeshi AI Chat</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6a28bd4b297" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why I Started Building Sudeshi AI Chat]]></title>
            <link>https://medium.com/@Sriram23/why-i-started-building-sudeshi-ai-chat-cf23033bf74f?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf23033bf74f</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[side-hustle]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Tue, 23 Jun 2026 15:53:05 GMT</pubDate>
            <atom:updated>2026-08-19T05:10:22.159Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z6K_NCHb8wzIrji8S8M9mQ.png" /></figure><p>For a long time, I wanted to build something outside of work. Not because I had a start-up idea, nor because I wanted to build a big AI product. I just wanted an interesting project that pushes me to learn and work on things I normally wouldn’t touch in my day-to-day work.</p><p>Around that time, I kept seeing AI products everywhere. Every week, there seems to be something new coming with the AI. I started thinking, “What if I build something with AI?” At first, I didn’t think too much; I just wanted a side project, just a chat screen connected to an API that returns responses.</p><h3>The First version</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uTxQtV_pTPSngUURHTUA8w.png" /><figcaption>The very first static version of Sudeshi</figcaption></figure><p>The first version was extremely simple. Type a message, send it, get the response, display it, that’s it. No abstraction, no optimization, no architecture, the goal was to get something working.</p><p>I expected AI to be the difficult part. But when I started building, I realized that the interesting problems are not with the AI.</p><p>I started getting these questions:</p><ul><li>How do I stream the responses smoothly?</li><li>What happens if there is a need to introduce another provider?</li><li>How do I make rendering efficient?</li><li>How to structure the state?</li><li>How do I keep the app maintainable?</li></ul><p>That’s when Sudeshi slowly stopped being just another chat app.</p><h3>From an AI project to an engineering playground</h3><p>Slowly, Sudeshi stopped becoming an AI experiment. It became a place to explore engineering ideas. I started trying things I don’t usually get to explore in my regular work, like streaming responses, abstraction layers, state management, performance improvements, architecture decisions, etc.</p><p>The goal is simple: Build something small and see how much I can learn from it.</p><h3>What does Sudeshi look like today?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nmBJATjvRnvZNh9bH6jkWw.png" /><figcaption>The current Sudeshi AI Chat (as of Jun 23rd, 2026)</figcaption></figure><p>Today, Sudeshi is a side project where I experiment with building production-style AI interfaces using Next.js. Here are some areas I focused on:</p><ul><li>Realtime Streaming</li><li>Support for multiple providers (Sarvam and hosted Ollama)</li><li>Persistent conversations in your browser</li><li>Performance Optimization</li><li>Testing and maintainability</li><li>Scalable chat rendering</li><li>Context compression</li><li>Runtime metrics</li><li>Multiple conversations</li><li>Long conversation performance</li></ul><p>and a few more things I’m still experimenting with. Now, I’m not trying to build another AI product. This project is mostly about exploring ideas and improving by building.</p><h3>What’s next?</h3><p>This article is more of an introduction. In the next few posts, I’ll write about the technical decisions and things that worked (and didn’t).</p><p>If you’re curious to explore what I built, feel free to play around with Sudeshi!</p><p>Live link:</p><p><a href="https://sudeshi-ai-chat.vercel.app/">Sudeshi</a></p><p>Source Code:</p><p><a href="https://github.com/sriram23/sudeshi-ai-chat">GitHub - sriram23/sudeshi-ai-chat: Powered by Sarvam</a></p><p>If you explore it and have suggestions, I’d love to hear them.</p><p>More related Stories:</p><ul><li><a href="https://medium.com/@Sriram23/the-moment-sudeshi-became-more-than-a-ui-wrapper-c6a28bd4b297">The Moment Sudeshi Became More Than a UI Wrapper</a></li><li><a href="https://medium.com/@Sriram23/how-i-added-streaming-to-sudeshi-ai-chat-02c63e6ffe3a">How I Added Streaming to Sudeshi AI Chat</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf23033bf74f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Thought Meetings Were Essential Until I Worked Across Time Zones]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/i-thought-meetings-were-essential-until-i-worked-across-time-zones-5720faa043e5?source=rss-1d2ca5d74008------2"><img src="https://cdn-images-1.medium.com/max/1774/1*mVy5MbRft4SwNy8suEVvgw.png" width="1774"></a></p><p class="medium-feed-snippet">When I joined the team, one thing surprised me almost immediately.</p><p class="medium-feed-link"><a href="https://medium.com/swlh/i-thought-meetings-were-essential-until-i-worked-across-time-zones-5720faa043e5?source=rss-1d2ca5d74008------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/i-thought-meetings-were-essential-until-i-worked-across-time-zones-5720faa043e5?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/5720faa043e5</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[work-culture]]></category>
            <category><![CDATA[remote-working]]></category>
            <category><![CDATA[productivity]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Tue, 16 Jun 2026 09:42:39 GMT</pubDate>
            <atom:updated>2026-06-16T09:42:39.978Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Four Engineering Lessons I Didn’t Learn From Frontend]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/four-engineering-lessons-i-didnt-learn-from-frontend-95c269a3581e?source=rss-1d2ca5d74008------2"><img src="https://cdn-images-1.medium.com/max/1448/1*kGi_W-UsN70Kwf81nlX6lA.png" width="1448"></a></p><p class="medium-feed-snippet">Support work, deployments, and distributed collaboration taught me lessons frontend never could.</p><p class="medium-feed-link"><a href="https://medium.com/swlh/four-engineering-lessons-i-didnt-learn-from-frontend-95c269a3581e?source=rss-1d2ca5d74008------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/four-engineering-lessons-i-didnt-learn-from-frontend-95c269a3581e?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/95c269a3581e</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[career-growth]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[lessons-learned]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Fri, 12 Jun 2026 15:06:49 GMT</pubDate>
            <atom:updated>2026-06-15T07:12:16.469Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[10 Mind-Boggling JavaScript Code Snippets That Will Confuse Even Experts!]]></title>
            <link>https://javascript.plainenglish.io/10-mind-boggling-javascript-code-snippets-that-will-confuse-even-experts-f308cc3ca0e2?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/f308cc3ca0e2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[jsquirks]]></category>
            <category><![CDATA[coding-tips]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Sat, 15 Feb 2025 12:34:05 GMT</pubDate>
            <atom:updated>2025-02-17T09:52:37.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LUBro75R6t5-O61z" /></figure><p>JavaScript is full of surprises! From tricky type coercions to unexpected object behaviors, even experienced developers can get caught off guard. In this blog, we’ll explore <strong>10 mind-boggling JavaScript code snippets</strong> that will challenge your understanding and sharpen your problem-solving skills. Whether you’re preparing for an interview or just love exploring JavaScript quirks, these examples will keep you on your toes.</p><p>Let’s dive in and decode the confusion! 🚀</p><h3>1. Truthy Math Operations</h3><h4>Code:</h4><pre>console.log(true + true * 7); // Output: 8</pre><h4>Explanation:</h4><p>Implicit type-coercion converts true to 1 when it encounters an arithmetic operator.</p><pre>true + true * 7 =&gt; 1 + 1 * 7 =&gt; 8</pre><h3>2. Array Sorting Gotcha</h3><h4>Code:</h4><pre>console.log([1, 5, 15].sort()); // Output: [1, 15, 5]</pre><h4>Explanation:</h4><p>In the sort method, numbers in the array are treated as strings and sorted <strong>lexicographically</strong>.<br>Lexicographically, &quot;1&quot; comes first, &quot;15&quot; comes next, and &quot;5&quot; comes last.</p><p>If we want to sort numerically instead of lexicographically, we should use:</p><pre>console.log([1, 5, 15].sort((a, b) =&gt; a - b)); // Output: [1, 5, 15]</pre><h3>3. Unexpected typeof Results</h3><h4>Code:</h4><pre>console.log(typeof NaN);  // Output: &quot;number&quot;<br>console.log(typeof null); // Output: &quot;object&quot;</pre><h4>Explanation:</h4><ul><li>The type of NaN is &quot;number&quot;, even though NaN stands for &quot;Not a Number.&quot;</li><li>In JavaScript, null is considered an <strong>object</strong>, even though it represents nothing.</li></ul><h3>4. The !! Trick</h3><h4>Code:</h4><pre>console.log(!!&quot;false&quot;); // Output: true<br>console.log(!!&quot;&quot;);      // Output: false<br>console.log(!!0);       // Output: false<br>console.log(!!1);       // Output: true</pre><h4>Explanation:</h4><p>The !! converts the following expression to a boolean:</p><ul><li>&quot;false&quot; → true (Non-empty strings are true.)</li><li>&quot;&quot; → false (Empty strings are false.)</li><li>0 → false</li><li>1 → true</li></ul><h3>5. Weird Coercion</h3><h4>Code:</h4><pre>console.log([] == ![]); // Output: true</pre><h4>Explanation:</h4><ul><li>![] converts the array to false.</li><li>When comparing an empty array ([]) with a boolean, JavaScript coerces both to numbers.</li><li>This results in:</li></ul><pre>  0 == 0 // true</pre><h3>6. Object Key Confusion</h3><h4>Code:</h4><pre>const obj = {<br>  1: &quot;one&quot;,<br>  0: &quot;zero&quot;,<br>  true: &quot;truth&quot;,<br>  false: &quot;false&quot;<br>};<br><br>console.log(obj[1]);      // Output: &quot;one&quot;<br>console.log(obj[true]);   // Output: &quot;truth&quot;</pre><h4>Explanation:</h4><p>Object keys are automatically converted to <strong>strings</strong> unless it’s a Symbol (Symbol will not be converted to strings. Thanks <a href="https://medium.com/u/dac8a3ab6853">Jon Randy</a> for enlightening this!).<br>When querying obj[true], it looks for the key &quot;true&quot;, not the true Boolean value.</p><h3>7. Function Hoisting Surprise</h3><h4>Code:</h4><pre>foo();  // Output: &quot;FOO&quot;<br>bar();  // Output: TypeError: bar is not a function<br><br>function foo() {<br>    console.log(&quot;FOO&quot;);<br>}<br><br>const bar = () =&gt; {<br>    console.log(&quot;BAR&quot;);<br>};</pre><h4>Explanation:</h4><p>During the <strong>memory creation phase</strong>, JavaScript initializes function declarations ( foo) with their full function definitions, whereas variables ( bar) are initialized with undefined.</p><p>Since bar is a variable holding an arrow function, calling bar() before its declaration results in a <strong>TypeError</strong>.</p><h3>8. Adding Objects?</h3><h4>Code:</h4><pre>console.log({} + []);  // Output: &quot;[object Object]&quot;<br>console.log([] + {});  // Output: &quot;[object Object]&quot;</pre><h4>Explanation:</h4><ul><li>[] (an empty array) is converted to an empty string (&quot;&quot;).</li><li>When adding an object to an empty string, the object gets coerced to &quot;[object Object]&quot;.</li></ul><h3>9. Deleting a Property vs. Variable</h3><h4>Code:</h4><pre>var a = 10;<br>delete a;<br>console.log(a);  // Output: 10<br><br>const obj = { b: 20 };<br>delete obj.b;<br>console.log(obj.b);  // Output: undefined</pre><h4>Explanation:</h4><ul><li>The delete operator works <strong>only</strong> with <strong>object properties</strong>, not variables.</li><li>delete a; does nothing, whereas delete obj.b; removes the property from the object.</li></ul><h3>10. Object Comparison Trap</h3><h4>Code:</h4><pre>const a = {};<br>const b = {};<br><br>console.log(a === b);  // Output: false<br>console.log(a == b);   // Output: false</pre><h4>Explanation:</h4><p>In JavaScript, objects are compared by <strong>reference, not by value</strong>.<br>Even though a and b are identical empty objects, they refer to two different memory locations, so the comparison returns false.</p><p>If we want to compare objects by value, we can use:</p><pre>JSON.stringify(a) === JSON.stringify(b);</pre><h3>Wrapping Up 🎯</h3><p>JavaScript is a language full of surprises, and these tricky code snippets are just the tip of the iceberg. Understanding these quirks not only helps you avoid common pitfalls but also sharpens your debugging skills-a must for every JavaScript developer!</p><p>Did any of these examples surprise you? 🤔 Or do you have your own favorite confusing JS snippet? <strong>Drop a comment below and let’s discuss!</strong> 💬</p><p>If you found this post helpful, don’t forget to <strong>share it with your fellow developers,</strong> and stay tuned for more JavaScript deep dives! 🚀</p><p><em>Originally published at </em><a href="https://sriram23.hashnode.dev/10-mind-boggling-javascript-code-snippets-that-will-confuse-even-experts"><em>https://sriram23.hashnode.dev</em></a><em> on February 15, 2025.</em></p><h3>Thank you for being a part of the community</h3><p><em>Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://x.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a> | <a href="https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0"><strong>Podcast</strong></a></li><li><a href="https://cofeed.app/"><strong>Check out CoFeed, the smart way to stay up-to-date with the latest in tech</strong></a> <strong>🧪</strong></li><li><a href="https://differ.blog/"><strong>Start your own free AI-powered blog on Differ</strong></a> 🚀</li><li><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Join our content creators community on Discord</strong></a> 🧑🏻‍💻</li><li>For more content, visit <a href="https://plainenglish.io/"><strong>plainenglish.io</strong></a> + <a href="https://stackademic.com/"><strong>stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f308cc3ca0e2" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/10-mind-boggling-javascript-code-snippets-that-will-confuse-even-experts-f308cc3ca0e2">10 Mind-Boggling JavaScript Code Snippets That Will Confuse Even Experts!</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Type Coercion in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/mastering-type-coercion-in-javascript-569c20cf7b4c?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/569c20cf7b4c</guid>
            <category><![CDATA[type-conversion]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-fundamental]]></category>
            <category><![CDATA[type-coercion]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Sun, 04 Aug 2024 13:59:38 GMT</pubDate>
            <atom:updated>2024-08-07T00:19:41.112Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d6KKQRISmNdQ9z-MMvTiVA.png" /></figure><p>Type coercion in JavaScript refers to the conversion of one type to another, either explicitly or implicitly. JavaScript is known for being a loosely typed language (sometimes referred to as ‘weakly typed,’ though I prefer not to use that term 😊). This means that JavaScript variables can hold values of any type without strict type definitions. Consequently, JavaScript often needs to convert variable types when performing operations.</p><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=twitter&amp;url=https%3A//x.com/RyanEls4/status/1810216892147114307&amp;image=" width="500" height="281" frameborder="0" scrolling="no"><a href="https://medium.com/media/11c790e16544ff5dcbe25438c9218460/href">https://medium.com/media/11c790e16544ff5dcbe25438c9218460/href</a></iframe><p>There are two types of Coercions:</p><ol><li>Explicit</li><li>Implicit</li></ol><h3>Explicit Coercion</h3><p>Explicit Coercion is when we manually convert values from one type to another using built-in functions or operators.</p><p><strong>Examples:</strong></p><pre>// Converting String to Number<br>const str = &quot;123&quot;;<br>const num = Number(str); // num is 123 (number)<br><br>// Converting Number to String<br>const num = 123;<br>const str = String(num); // str is &quot;123&quot; (string)<br><br>// Converting Boolean to Number<br>const bool = true;<br>const num = Number(bool); // num is 1 (number)</pre><h3>Real World Scenarios</h3><h4>Form Input Validation</h4><p>In form fields like text box, we will be getting the values as String. Validation for field can be done with explicit type coercion</p><pre>const ageInput = document.getElementById(&quot;age&quot;).value; // &quot;25&quot;<br>const age = Number(ageInput); // 25<br><br>if (isNaN(age)) {<br>  alert(&quot;Please enter a valid number for age.&quot;);<br>} else {<br>  console.log(`User&#39;s age is ${age}`);<br>}</pre><h4>Parsing JSON</h4><p>In some cases, while fetching JSON data from APIs, it will be String, that we need to convert into Object explicitly.</p><pre>const jsonResponse = &#39;{&quot;name&quot;: &quot;Sriram&quot;, &quot;age&quot;: 28}&#39;;<br>const user = JSON.parse(jsonResponse);<br><br>console.log(user.name); // &quot;Sriram&quot;<br>console.log(user.age);  // 28</pre><h3>Handling Local Storage/Session Storage</h3><p>Local Storage and Session Storage will accept string values only. If we want to store any non-string value like a number, we will have to explicitly convert it to String.</p><pre>const id=189764<br>sessionStorage.setItem(&#39;id&#39;, String(id))</pre><h3>Implicit Coercion</h3><p>Implicit Coercion happens when we try to perform some operations. JavaScript will automatically convert the types in order to perform the operation.</p><p><strong>Examples:</strong></p><pre>// String Concatenation<br>const num = 42;<br>const result = &quot;The answer is &quot; + num; // &quot;The answer is 42&quot;<br>const str = 5 + &quot; apples&quot;; // &quot;5 apples&quot;<br><br>// Arithmetic Operations<br>const result1 = &quot;5&quot; - 2; // 3 (string &quot;5&quot; is converted to number 5)<br>const result2 = &quot;6&quot; * 2; // 12 (string &quot;6&quot; is converted to number 6)<br>const result3 = &quot;10&quot; / 2; // 5 (string &quot;10&quot; is converted to number 10)<br>const result4 = &quot;10&quot; % 3; // 1 (string &quot;10&quot; is converted to number 10)<br><br>// Comparison Operations<br>const result1 = &quot;10&quot; &gt; 5; // true (string &quot;10&quot; is converted to number 10)<br>const result2 = &quot;20&quot; &lt; 15; // false (string &quot;20&quot; is converted to number 20)<br>const result3 = &quot;5&quot; &lt;= 5; // true (string &quot;5&quot; is converted to number 5)<br>const result4 = &quot;3&quot; &gt;= 2; // true (string &quot;3&quot; is converted to number 3)<br><br>// Equality Comparisons<br>const result1 = 1 == &quot;1&quot;; // true (string &quot;1&quot; is converted to number 1)<br>const result2 = true == 1; // true (boolean true is converted to number 1)<br>const result3 = false == 0; // true (boolean false is converted to number 0)<br>const result4 = null == undefined; // true (both are considered equal)<br><br>// Logical Operators<br>const result1 = &quot;hello&quot; &amp;&amp; 0; // 0 (string &quot;hello&quot; is truthy, so returns 0)<br>const result2 = &quot;&quot; || &quot;default&quot;; // &quot;default&quot; (empty string is falsy, so returns &quot;default&quot;)<br>const result3 = !0; // true (number 0 is falsy, so returns true)<br>const result4 = !&quot;non-empty string&quot;; // false (non-empty string is truthy, so returns false)</pre><h3>Common Pitfalls</h3><p>Type coercion may lead to unexpected results, that will lead to bugs. Understanding on how JavaScript handles coercion can help to avoid such errors.</p><h3>Best Practices</h3><ul><li>Use strict equality (===) and inequality (!==) operators to prevent any unintended type coercion</li><li>Make use of explicit type coercion functions/operators wherever we are sure about the type.</li><li>Understand the <strong>truthy</strong> and <strong>falsy</strong> values to avoid logical errors.</li><li>Use <strong>Typescript</strong>, which makes the language Strongly typed.</li></ul><p>Thank you for reading! I appreciate your time and hope you found this post valuable. Please let me know your thoughts in the comments below!</p><p><em>Originally published at </em><a href="https://sriram23.hashnode.dev/mastering-type-coercion-in-javascript"><em>https://sriram23.hashnode.dev</em></a><em> on August 4, 2024.</em></p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=569c20cf7b4c" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/mastering-type-coercion-in-javascript-569c20cf7b4c">Mastering Type Coercion in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Currying in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/currying-in-javascript-98825b7f2413?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/98825b7f2413</guid>
            <category><![CDATA[currying]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[closure-in-javascript]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Sat, 03 Aug 2024 02:18:20 GMT</pubDate>
            <atom:updated>2024-08-07T00:19:35.136Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PklxupncXhx4uQiA" /></figure><p>Currying is a technique in JavaScript, where the function with multiple arguments is transformed into a series of functions, each taking one argument.</p><h3>Why Currying?</h3><ul><li><strong>Flexibility</strong>: It allows us to create new functions from existing functions, presetting some arguments and leaving other open.</li><li><strong>Reusability</strong>: Functions can be reused with some arguments pre-filled.</li><li><strong>Simplicity</strong>: It makes easier to code when dealing with complex logics.</li></ul><h3>How to achieve?</h3><p>Currying in JavaScript can be achieved in two ways:</p><ul><li>Using <strong>Bind</strong></li><li>Using <strong>Closures</strong></li></ul><p>Let us explore both the implementations using a simple multiplication function that takes two parameters num1 and num2. Now I need a function that multiplies a number by two, which means it requires a single parameter and the provided number will be multiplied by 2. Let us use currying to solve this.</p><h3>Bind implementation</h3><p>Let’s have a function that multiplies two numbers</p><pre>let multiply = (num1, num2) =&gt; {<br>    return num1 * num2<br>}</pre><p>This is how we can use currying to implement our second function multiplyBy2.</p><pre>let multiplyBy2 = multiply.bind(this, 2)</pre><p>This is how the functions can be invoked</p><pre>multiply(2, 3) // Returns 6<br>multiplyBy2(4) // Returns 8</pre><h3>Closure implementation</h3><p>In case of closure, we will need to have to modify the multiply function in such a way that it has an inner function for the second argument.</p><pre>let multiply = function(x) {<br>    return function(y) {<br>        return x*y<br>    }<br>}</pre><p>Here the inner function has the access to the scope of it’s parent function. This is called <strong>closure</strong> in JavaScript. Let’s implement our multiplyBy2 function here.</p><pre>let multiplyBy2 = multiply(2)</pre><p>This is how the functions can be invoked</p><pre>multiply(2)(3) // Returns 6<br>multiplyBy2(4) // Returns 8</pre><p>This is how Currying can be implemented in JavaScript.</p><h3>Use Cases</h3><p>One real world example where we can use Currying is with logging function implementation. The function will take two arguments Level and Message. We can use Currying to create functions for different log levels. Let us see how we can implement.</p><pre>const logger = (level) =&gt; (message) =&gt; `[${level}] ${message}`<br><br>// Currying<br>const debug = logger(&#39;debug&#39;)<br>const info = logger(&#39;info&#39;)<br>const warning = logger(&#39;warning&#39;)<br>const error =  logger(&#39;error&#39;)<br><br>// Functions invocation<br>debug(&quot;Checking if the values is coming&quot;)  // Returns &quot;[debug] Checking if the values is coming&quot;<br>info(&quot;API called successfully&quot;)  // Returns &quot;[info] API called successfully&quot;<br>warning(&quot;Server returned a warning message&quot;)  // Retuns &quot;[warning] Server returned a warning message&quot;<br>error(&quot;Something went wrong while calling the API&quot;)  // returns &quot;[error] Something went wrong while calling the API&quot;</pre><p>Thank you for reading! Appreciate your time and hope you found this post valuable. Give currying a try in your next JavaScript project and let me know how it improves your coding experience in the comments below!</p><p><em>Originally published at </em><a href="https://sriram23.hashnode.dev/currying-in-javascript"><em>https://sriram23.hashnode.dev</em></a><em> on August 3, 2024.</em></p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a> | <a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98825b7f2413" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/currying-in-javascript-98825b7f2413">Currying in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unveiling Containers: A Comparative Analysis of Real-World Containers and Software Containers]]></title>
            <link>https://medium.com/@Sriram23/unveiling-containers-a-comparative-analysis-of-real-world-containers-and-software-containers-fa24a8777b0c?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa24a8777b0c</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[archived]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Thu, 22 Jun 2023 02:42:37 GMT</pubDate>
            <atom:updated>2023-06-22T02:42:37.337Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F2ccUrmxwlBKl8QH" /></figure><p>In the world of modern application development and deployment, containers have gained immense popularity. They provide lightweight and portable solutions for packaging and running applications. In this post, we will see in detail about the containers.</p><h3>Real-world Containers vs. Software Containers</h3><p>The real-world containers are the physical structures, that are designed to transport goods and materials. These containers are used in industries like warehousing, shipping, etc. These containers are made of robust materials, that could be transported in a variety of transportation.</p><p>Software Containers on the other hand are a virtualization technology, which provides a lightweight isolated environment for applications and their dependencies.</p><h3>How container works?</h3><p>The containers leverage the host operating system’s kernel and resources. These lightweight environments separate the application within it and its dependencies from the host environment and other containers. The host os provides necessary resources to the container like storage, networking and system libraries.</p><h3>Are Containers the Same as VMs?</h3><p>Containers and virtual machines (VMs) serve different purposes and have distinct characteristics. Containers offer lightweight isolation at the operating system level, sharing the host’s kernel. They provide faster startup times, efficient resource utilization, and higher density compared to VMs. VMs, on the other hand, create a completely virtualized environment with a separate guest operating system, allowing multiple operating systems to run on a single physical host. VMs provide stronger isolation but are typically heavier in terms of resource usage.</p><h3>Pros and Cons of Containers</h3><h3>Pros</h3><ol><li>Portability: Containers provide consistent deployment and execution across different environments, making it easier to move applications between the development, testing, and production stages.</li><li>Scalability: Containers enable easy horizontal scaling, allowing applications to handle increased workloads efficiently.</li><li>Efficiency: Containers have a smaller footprint, faster startup times, and efficient resource utilization, enabling higher density on host machines.</li><li>Isolation: Containers offer process and file system isolation, ensuring that applications and their dependencies are encapsulated and don’t interfere with each other.</li><li>DevOps-Friendly: Containers facilitate the adoption of DevOps practices, enabling faster application development, deployment, and continuous integration.</li></ol><h3>Cons</h3><ol><li>Limited Compatibility: Applications requiring low-level kernel access or specific hardware dependencies might face challenges when running inside containers.</li><li>Networking Complexity: Container networking requires careful configuration and management to ensure proper communication between containers and with the outside world.</li><li>Security Considerations: Container security practices, such as image scanning, isolation, and access control, are crucial to prevent potential vulnerabilities.</li></ol><p>Containers have revolutionized the way applications are developed, deployed, and managed. They offer lightweight and portable environments, simplifying the process of running applications consistently across different platforms. By embracing containers and leveraging their advantages while addressing the associated challenges, organizations can unlock greater efficiency and flexibility in their application ecosystems.</p><p><em>Originally published at </em><a href="https://sriram23.hashnode.dev/unveiling-containers-a-comparative-analysis-of-real-world-containers-and-software-containers"><em>https://sriram23.hashnode.dev</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa24a8777b0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Functions and Callbacks in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/understanding-functions-and-callbacks-in-javascript-f288bf19bc2e?source=rss-1d2ca5d74008------2</link>
            <guid isPermaLink="false">https://medium.com/p/f288bf19bc2e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[archived]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[functions-in-javascript]]></category>
            <dc:creator><![CDATA[Sriram]]></dc:creator>
            <pubDate>Fri, 24 Mar 2023 16:13:20 GMT</pubDate>
            <atom:updated>2023-03-24T16:13:20.736Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tskSfXmhO7CyNjLcNMof4g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@6heinz3r?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gabriel Heinzer</a> on <a href="https://unsplash.com/photos/g5jpH62pwes?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Hello World!</p><p>Functions are a key feature of JavaScript programming that allow developers to create modular, reusable code. In this post, we’ll explore the basics of functions in JavaScript, including function declarations, function expressions, and named function expressions. We’ll also discuss the concept of callbacks, a powerful technique used in asynchronous programming to manage the flow of execution and to handle events or responses that are not known in advance.</p><h3>Functions in JavaScript</h3><p>A function in JavaScript is a block of code that can be executed on demand. Functions can take input values (called arguments) and can return output values. In JavaScript, functions can be defined using function declarations, function expressions, and arrow functions.</p><h3>Function Declarations</h3><p>A function declaration defines a named function using the following syntax:</p><pre>function functionName(arguments) {<br>  // function code goes here<br>  return value;<br>}</pre><p>The functionName is the name of the function, and arguments are the parameters that the function takes. The function code goes inside the curly braces, and the return statement specifies the value that the function should return.</p><p>Example:</p><pre>function addNumbers(val1, val2) {<br>  return val1 + val2;<br>}</pre><h3>Function Expressions</h3><p>A function expression defines an unnamed function that is assigned to a variable, using the following syntax:</p><pre>const functionName = function(arguments) {<br>  // function code goes here<br>  return value;<br>};</pre><p>In this case, the function is defined as an anonymous function and is assigned to the variable functionName. The function code goes inside the curly braces, and the return statement specifies the value that the function should return.</p><p>Example:</p><pre>const addNumbers = function(val1, val2) {<br>  return val1 + val2;<br>}</pre><h3>Named Function Expressions</h3><p>A named function expression is similar to a function expression, but it defines a named function that is assigned to a variable, using the following syntax:</p><pre>const func = function functionName(arguments) {<br>  // function code goes here<br>  return value;<br>};</pre><p>The function has a name (functionName in this example), which can be useful for debugging.</p><h3>Callbacks in JavaScript</h3><p>A callback is a function that is passed as an argument to another function and is executed by that function when a certain event or condition is met. Callbacks are a powerful technique used in asynchronous programming to manage the flow of execution and to handle events or responses that are not known in advance.</p><p>Here’s an example of using a callback in JavaScript:</p><pre>function addNumbers(a, b, callback) {<br>  const sum = a + b;<br>  callback(sum);<br>}<br><br>function displayResult(result) {<br>  console.log(&#39;The sum is &#39; + result);<br>}<br><br>addNumbers(5, 10, displayResult);</pre><p>In this example, we call the addNumbers function with two arguments (5 and 10) and a callback function displayResult. The addNumbers function calculates the sum of the two arguments and passes the result to the callback function. The callback function then logs the result to the console.</p><p>Functions and callbacks are powerful tools in JavaScript that allow developers to create modular, reusable code and manage asynchronous operations. By understanding the basics of functions and callbacks, developers can create efficient and scalable code that can handle complex operations. In addition, understanding how callbacks work is essential for working with APIs, handling user input, and creating interactive web applications.</p><p>That’s all about the functions and callbacks in JavaScript. I hope you found this useful. Thanks for reading!</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Join our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em> community and follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em> and</em><strong><em> </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em>.</em></strong></p><p><strong><em>Learn how to build awareness and adoption for your startup with </em></strong><a href="https://circuit.ooo/?utm=publication-post-cta"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f288bf19bc2e" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/understanding-functions-and-callbacks-in-javascript-f288bf19bc2e">Understanding Functions and Callbacks in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>