<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Peeyush Tyagi</title>
    <description>The latest articles on DEV Community by Peeyush Tyagi (@peeyush_tyagi_473a6b7f1e7).</description>
    <link>https://dev.to/peeyush_tyagi_473a6b7f1e7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2795852%2F7aee3b30-b62f-41d1-bf31-d7602100453f.png</url>
      <title>DEV Community: Peeyush Tyagi</title>
      <link>https://dev.to/peeyush_tyagi_473a6b7f1e7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/peeyush_tyagi_473a6b7f1e7"/>
    <language>en</language>
    <item>
      <title>🚀 JavaScript Debugging Challenge: Fixing a Closure Bug</title>
      <dc:creator>Peeyush Tyagi</dc:creator>
      <pubDate>Mon, 03 Feb 2025 18:08:29 +0000</pubDate>
      <link>https://dev.to/peeyush_tyagi_473a6b7f1e7/javascript-debugging-challenge-fixing-a-closure-bug-16d5</link>
      <guid>https://dev.to/peeyush_tyagi_473a6b7f1e7/javascript-debugging-challenge-fixing-a-closure-bug-16d5</guid>
      <description>&lt;p&gt;Problem Statement&lt;br&gt;
You're given the following JavaScript function that is supposed to print numbers 1 to 5, each with a 1-second delay. But something is wrong!&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
function printNumbers() {&lt;br&gt;
    for (var i = 1; i &amp;lt;= 5; i++) {&lt;br&gt;
        setTimeout(function() {&lt;br&gt;
            console.log(i);&lt;br&gt;
        }, i * 1000);&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;printNumbers();&lt;br&gt;
Expected Output:&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;br&gt;
4&lt;br&gt;
5&lt;br&gt;
Actual Bugged Output:&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
🔎 What’s the Bug?&lt;br&gt;
The issue is caused by closures and the var keyword.&lt;br&gt;
setTimeout stores a reference to i, but by the time the callback executes, the loop has already finished and i = 6.&lt;br&gt;
Hence, all iterations log 6 instead of 1, 2, 3, 4, 5.&lt;br&gt;
✅ How to Fix It?&lt;br&gt;
Solution 1: Use let instead of var (Block Scope Fix)&lt;br&gt;
javascript&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
function printNumbers() {&lt;br&gt;
    for (let i = 1; i &amp;lt;= 5; i++) {&lt;br&gt;
        setTimeout(function() {&lt;br&gt;
            console.log(i);&lt;br&gt;
        }, i * 1000);&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
printNumbers();&lt;br&gt;
🔹 let creates a block-scoped variable for each loop iteration, fixing the issue.&lt;/p&gt;

&lt;p&gt;Solution 2: Use an IIFE (Immediately Invoked Function Expression)&lt;br&gt;
javascript&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
function printNumbers() {&lt;br&gt;
    for (var i = 1; i &amp;lt;= 5; i++) {&lt;br&gt;
        (function(num) {&lt;br&gt;
            setTimeout(function() {&lt;br&gt;
                console.log(num);&lt;br&gt;
            }, num * 1000);&lt;br&gt;
        })(i);&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
printNumbers();&lt;br&gt;
🔹 The IIFE captures i as num, preserving the correct value for each iteration.&lt;/p&gt;

&lt;p&gt;⚡ Final Thoughts&lt;br&gt;
Understanding closures and scope is crucial for mastering JavaScript! Which solution do you prefer? Let me know in the comments! 💬🔥&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript #Closures #Debugging #WebDevelopment
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>🚀 Understanding React Context and the Double Curly Braces in AuthContext.Provider 💡</title>
      <dc:creator>Peeyush Tyagi</dc:creator>
      <pubDate>Fri, 31 Jan 2025 18:08:34 +0000</pubDate>
      <link>https://dev.to/peeyush_tyagi_473a6b7f1e7/understanding-react-context-and-the-double-curly-braces-in-authcontextprovider-3mf3</link>
      <guid>https://dev.to/peeyush_tyagi_473a6b7f1e7/understanding-react-context-and-the-double-curly-braces-in-authcontextprovider-3mf3</guid>
      <description>&lt;p&gt;In React, the Context API is a game-changer when it comes to managing global state across your components. One part of React Context that often trips up developers is the use of the Provider component and its value prop. Specifically, you might have encountered this line:&lt;/p&gt;

&lt;p&gt;jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
&lt;br&gt;
But why does this line contain double curly braces? Let’s break it down!&lt;/p&gt;

&lt;p&gt;1️⃣ React Context Overview&lt;br&gt;
The Context API allows you to pass values (like user data, authentication state, themes, etc.) through your component tree without manually passing props to each intermediate level.&lt;/p&gt;

&lt;p&gt;2️⃣ The Double Curly Braces&lt;br&gt;
Outer {}: These are used to embed JavaScript expressions within JSX. In React, whenever you want to execute JavaScript inside JSX, you wrap it in curly braces.&lt;/p&gt;

&lt;p&gt;Inner {}: This is where we define a JavaScript object. In this case, it’s an object containing useData and admin. You’re passing this object as the value to the Provider to share it with the child components.&lt;/p&gt;

&lt;p&gt;Here's the breakdown:&lt;/p&gt;

&lt;p&gt;jsx&lt;br&gt;
Copy&lt;br&gt;
Edit&lt;br&gt;
&lt;br&gt;
The outer {} is needed because we are using JSX. The inner {} creates an object to pass useData and admin as values to the Provider.&lt;/p&gt;

&lt;p&gt;3️⃣ Why It Matters&lt;br&gt;
Using Context in this way allows components deeper in the component tree to access the values shared via the Provider, avoiding "prop drilling" and making the code cleaner and more maintainable.&lt;/p&gt;

&lt;p&gt;🔗 Key Takeaways:&lt;/p&gt;

&lt;p&gt;The Context API is a powerful tool for managing global state in React.&lt;br&gt;
The double curly braces are necessary for embedding a JavaScript object in JSX.&lt;br&gt;
It makes managing shared state between components much more efficient.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
