<?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: ElyasShamal</title>
    <description>The latest articles on DEV Community by ElyasShamal (@elyasshamal).</description>
    <link>https://dev.to/elyasshamal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1075757%2F433bbb51-a994-4335-bc90-b75c57e21556.jpg</url>
      <title>DEV Community: ElyasShamal</title>
      <link>https://dev.to/elyasshamal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elyasshamal"/>
    <language>en</language>
    <item>
      <title>Exploring Different Ways to Fetch Data in React</title>
      <dc:creator>ElyasShamal</dc:creator>
      <pubDate>Mon, 09 Oct 2023 00:46:01 +0000</pubDate>
      <link>https://dev.to/elyasshamal/exploring-different-ways-to-fetch-data-in-react-2lid</link>
      <guid>https://dev.to/elyasshamal/exploring-different-ways-to-fetch-data-in-react-2lid</guid>
      <description>&lt;p&gt;## &lt;strong&gt;Introduction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fetching and displaying data is a critical component of creating interactive and dynamic user interfaces in modern web development. React, a popular JavaScript toolkit for creating user interfaces includes a number of methods and mechanisms for retrieving data from diverse sources. In this blog, we will look at various methods for retrieving data in React and examine their benefits and use cases.&lt;/p&gt;

&lt;p&gt;1.## &lt;strong&gt;Using the &lt;code&gt;fetch&lt;/code&gt; API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The native fetch API is the simplest and most frequent way to fetch data in React. It enables you to send HTTP requests and get responses asynchronously. Here's an easy way to use it in a React component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    fetch('https://api.example.com/data')
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; setData(data))
      .catch((error) =&amp;gt; console.error('Error:', error));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simple and widely supported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Suitable for basic data fetching requirements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.## &lt;strong&gt;Using the &lt;code&gt;axios&lt;/code&gt; library&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Axios&lt;/code&gt; is a popular JavaScript library for making HTTP requests. It provides a more robust and feature-rich alternative to the fetch API. To utilize &lt;code&gt;axios&lt;/code&gt; in a React component, first install it and then import it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';
import axios from 'axios';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    axios.get('https://api.example.com/data')
      .then((response) =&amp;gt; setData(response.data))
      .catch((error) =&amp;gt; console.error('Error:', error));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Provides a more extensive set of features for handling requests and responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Offers built-in support for interceptors, request cancellation, and more.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.##&lt;strong&gt;Using React's useState and useEffect with async/await&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also fetch data using async/await within a useEffect hook, which can make your code more readable and maintainable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
        setData(data);
      } catch (error) {
        console.error('Error:', error);
      }
    };

    fetchData();
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cleaner and more readable code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better support for error handling using try/catch.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.## &lt;strong&gt;Using third-party state management libraries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To control the data fetching process in complicated apps, you may want to employ state management libraries like Redux or Mobx. These libraries provide a centralized approach to handle data fetching and state management across multiple components.&lt;/p&gt;

&lt;p&gt;While setting up and utilizing these libraries can be more difficult, they have advantages such as greater scalability and maintainability in larger applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Data fetching is a critical component of developing dynamic React applications. The method you use to retrieve data is determined by the complexity of your project, your specific requirements, and your familiarity with the available tools.&lt;/p&gt;

&lt;p&gt;We looked at how to fetch data in React using the fetch API, the axios library, async/await, and third-party state management libraries in this blog. Each approach has advantages and disadvantages, and which one to use should be determined by the specific requirements of your project.&lt;/p&gt;

&lt;p&gt;When implementing data fetching in your React applications, keep in mind that error handling, data caching, and performance considerations are all important factors to consider. Select the approach that best fits your use case and assists you in creating efficient and responsive user interfaces.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>api</category>
      <category>axioslibrary</category>
    </item>
    <item>
      <title>Exploring Different Ways to Fetch Data in React</title>
      <dc:creator>ElyasShamal</dc:creator>
      <pubDate>Mon, 09 Oct 2023 00:46:01 +0000</pubDate>
      <link>https://dev.to/elyasshamal/exploring-different-ways-to-fetch-data-in-react-3e5h</link>
      <guid>https://dev.to/elyasshamal/exploring-different-ways-to-fetch-data-in-react-3e5h</guid>
      <description>&lt;p&gt;## &lt;strong&gt;Introduction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fetching and displaying data is a critical component of creating interactive and dynamic user interfaces in modern web development. React, a popular JavaScript toolkit for creating user interfaces includes a number of methods and mechanisms for retrieving data from diverse sources. In this blog, we will look at various methods for retrieving data in React and examine their benefits and use cases.&lt;/p&gt;

&lt;p&gt;1.## &lt;strong&gt;Using the &lt;code&gt;fetch&lt;/code&gt; API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The native fetch API is the simplest and most frequent way to fetch data in React. It enables you to send HTTP requests and get responses asynchronously. Here's an easy way to use it in a React component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    fetch('https://api.example.com/data')
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; setData(data))
      .catch((error) =&amp;gt; console.error('Error:', error));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simple and widely supported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Suitable for basic data fetching requirements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.## &lt;strong&gt;Using the &lt;code&gt;axios&lt;/code&gt; library&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Axios&lt;/code&gt; is a popular JavaScript library for making HTTP requests. It provides a more robust and feature-rich alternative to the fetch API. To utilize &lt;code&gt;axios&lt;/code&gt; in a React component, first install it and then import it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';
import axios from 'axios';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    axios.get('https://api.example.com/data')
      .then((response) =&amp;gt; setData(response.data))
      .catch((error) =&amp;gt; console.error('Error:', error));
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Provides a more extensive set of features for handling requests and responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Offers built-in support for interceptors, request cancellation, and more.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.##&lt;strong&gt;Using React's useState and useEffect with async/await&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also fetch data using async/await within a useEffect hook, which can make your code more readable and maintainable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function DataFetchingComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
        setData(data);
      } catch (error) {
        console.error('Error:', error);
      }
    };

    fetchData();
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {/* Display data here */}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cleaner and more readable code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better support for error handling using try/catch.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.## &lt;strong&gt;Using third-party state management libraries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To control the data fetching process in complicated apps, you may want to employ state management libraries like Redux or Mobx. These libraries provide a centralized approach to handle data fetching and state management across multiple components.&lt;/p&gt;

&lt;p&gt;While setting up and utilizing these libraries can be more difficult, they have advantages such as greater scalability and maintainability in larger applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Data fetching is a critical component of developing dynamic React applications. The method you use to retrieve data is determined by the complexity of your project, your specific requirements, and your familiarity with the available tools.&lt;/p&gt;

&lt;p&gt;We looked at how to fetch data in React using the fetch API, the axios library, async/await, and third-party state management libraries in this blog. Each approach has advantages and disadvantages, and which one to use should be determined by the specific requirements of your project.&lt;/p&gt;

&lt;p&gt;When implementing data fetching in your React applications, keep in mind that error handling, data caching, and performance considerations are all important factors to consider. Select the approach that best fits your use case and assists you in creating efficient and responsive user interfaces.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>api</category>
      <category>axioslibrary</category>
    </item>
    <item>
      <title>Generating Unique IDs with crypto.randomUUID() in React:</title>
      <dc:creator>ElyasShamal</dc:creator>
      <pubDate>Sat, 01 Jul 2023 02:30:08 +0000</pubDate>
      <link>https://dev.to/elyasshamal/generating-unique-ids-with-cryptorandomuuid-in-react-2d1e</link>
      <guid>https://dev.to/elyasshamal/generating-unique-ids-with-cryptorandomuuid-in-react-2d1e</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Working with React applications frequently requires the generation of unique identifiers (IDs) for a variety of functions, such as managing lists, tracking components, or handling form inputs. In this blog post, we'll look at how to generate unique IDs in React using the &lt;code&gt;crypto.randomUUID()&lt;/code&gt; function. We'll go through the advantages of using &lt;code&gt;crypto.randomUUID()&lt;/code&gt; and present practical examples to help you use it efficiently in your React projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use &lt;code&gt;crypto.randomUUID()&lt;/code&gt;?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The crypto.randomUUID() function is a built-in technique in current JavaScript environments for generating a cryptographically safe universally unique identifier (UUID). It assures that the generated IDs are extremely likely to be unique across several systems and applications. By using &lt;code&gt;crypto.randomUUID()&lt;/code&gt; in React, you can eliminate the requirement for external libraries and simplify the process of creating unique IDs.&lt;/p&gt;

&lt;p&gt;Implementing &lt;code&gt;crypto.randomUUID()&lt;/code&gt; in React: Let's look at how to use &lt;code&gt;crypto.randomUUID()&lt;/code&gt; in React to generate unique IDs in various contexts.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example 1: Generating IDs for List Items&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Each item in a dynamic list of components or elements normally requires a unique key or ID. In React, here's an example of how to generate unique IDs for a list of items:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';

const ListComponent = () =&amp;gt; {
  const [items, setItems] = useState([]);

  useEffect(() =&amp;gt; {
    // Generate unique IDs for list items
    const updatedItems = Array(5).fill().map(() =&amp;gt; ({
      id: crypto.randomUUID(),
      name: 'Example Item',
    }));
    setItems(updatedItems);
  }, []);

  return (
    &amp;lt;ul&amp;gt;
      {items.map((item) =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;crypto.randomUUID()&lt;/code&gt; function is called to generate a unique ID for each list item. The IDs are then used as keys for efficient rendering and updating of the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example 2: Creating Unique Form Input IDs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When working with forms, it's common to need to use the &lt;code&gt;for&lt;/code&gt; attribute to associate labels with input fields. To ensure unique &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt; properties, you can use &lt;code&gt;crypto.randomUUID()&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRef } from 'react';

const FormComponent = () =&amp;gt; {
  const inputRef = useRef(null);
  const inputId = crypto.randomUUID();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;label htmlFor={inputId}&amp;gt;Example Input&amp;lt;/label&amp;gt;
      &amp;lt;input id={inputId} ref={inputRef} type="text" /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Crypto.randomUUID()&lt;/code&gt; is used in this example to produce a unique ID (&lt;code&gt;inputId&lt;/code&gt;) for the input field. The &lt;code&gt;inputId&lt;/code&gt; is then utilized for both the label's &lt;code&gt;htmlFor&lt;/code&gt; attribute and the input element's id attribute, guaranteeing proper correlation between the two.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Benefits of &lt;code&gt;crypto.randomUUID()&lt;/code&gt;:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Universally unique: &lt;code&gt;crypto.randomUUID()&lt;/code&gt; generates IDs that are highly likely to be unique across different systems and applications, reducing the risk of collisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cryptographically secure: The generated IDs are based on a cryptographically strong random number generator, ensuring a high level of randomness and security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built-in functionality: &lt;code&gt;crypto.randomUUID()&lt;/code&gt; is available without the requirement for additional libraries in modern JavaScript environments, simplifying your development process.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;By utilizing &lt;code&gt;crypto.randomUUID()&lt;/code&gt; in your React applications, you can generate unique IDs in a secure and efficient manner. Whether you need unique keys for list rendering or IDs for form inputs, &lt;code&gt;crypto.randomUUID()&lt;/code&gt; provides a reliable solution. Embrace the power of &lt;code&gt;crypto.randomUUID()&lt;/code&gt; to simplify your React development and ensure the uniqueness and security of your identifiers.&lt;/p&gt;

</description>
      <category>react</category>
      <category>ids</category>
      <category>beginners</category>
      <category>flatiron</category>
    </item>
    <item>
      <title>What are JavaScript Cookies?</title>
      <dc:creator>ElyasShamal</dc:creator>
      <pubDate>Wed, 03 May 2023 02:34:42 +0000</pubDate>
      <link>https://dev.to/elyasshamal/what-are-javascript-cookies-536p</link>
      <guid>https://dev.to/elyasshamal/what-are-javascript-cookies-536p</guid>
      <description>&lt;p&gt;JavaScript cookies are small data files that a web server stores on a user's computer. They can be used to store data like user preferences, login details, and goods from shopping carts. In this article, we'll examine JavaScript cookies in more detail and give several usage examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cookie Creation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The document.cookie attribute in JavaScript can be used to create cookies. The following is the syntax for creating a cookie:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.cookie = "cookieName=cookieValue; expires=cookieExpiryDate;
path=cookiePath";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;cookieName: The name of the cookie you want to create.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cookieValue: The value you want to assign to the cookie.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cookieExpiryDate: The cookie's expiration date and time (optional).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;cookiePath: The cookie's availability path (optional).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take the example of wanting to build a cookie to remember a user's name. To accomplish this, set "username" as the cookieName and "username" as the cookieValue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.cookie = "username=Elyas";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reading a Cookie:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The document.cookie property can be used to read a cookie in JavaScript. The cookie property of the document returns a string containing all of the cookies for the current document. This text can then be parsed to find the value of a specific cookie.&lt;/p&gt;

&lt;p&gt;Assume you want to get the value of the "username" cookie we created earlier. This can be accomplished as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cookies = document.cookie.split(';');
for(let i=0; i &amp;lt; cookies.length; i++) {
  let cookie = cookies[i].trim();
  if (cookie.startsWith("username=")) {
    let username = cookie.substring("username=".length, cookie.length);
    console.log("Username is " + username);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use semicolons to separate the document.cookie string into individual cookies. Then we loop through each cookie and see if it begins with "username=". If it does, we extract the cookie value and log it to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deleting a Cookie:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To delete a cookie in JavaScript, change the cookie's expiry date to a date in the past. This will result in the cookie being erased.&lt;/p&gt;

&lt;p&gt;Assume you want to get rid of the "username" cookie. You can accomplish this by doing the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.cookie = "username=;
expires=Thu, 01 Jan 1990 00:00:00 UTC; path=/;";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We set the "username" cookie's value to an empty string, set the expiration date to a date in the past, and set the path to "/" to ensure that the cookie is erased from all pathways.&lt;/p&gt;

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

&lt;p&gt;JavaScript cookies are a valuable resource for web developers. They can be used to save user information, preferences, and goods from the shopping cart. Cookies allow you to deliver a personalized experience for your users while also improving the functionality of your website. In this blog, we demonstrated how cookies may be generated, read, and removed in JavaScript.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>cookies</category>
      <category>flatiron</category>
    </item>
  </channel>
</rss>
