<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Casmir Onyekani - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Casmir Onyekani - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 22:23:48 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/Casmir/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Build High-Ranking SEO Landing Page ]]>
                </title>
                <description>
                    <![CDATA[ New products are dropping daily, and smart sellers are quietly stacking profits. If you understand how SEO landing pages actually work today, you’re not guessing. Instead, you're building assets that  ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-high-ranking-seo-landing-page/</link>
                <guid isPermaLink="false">69fa212aa386d7f121b568d8</guid>
                
                    <category>
                        <![CDATA[ SEO ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Tue, 05 May 2026 16:56:10 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/11f1d667-12a7-4097-8583-eb1a8f87bf02.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>New products are dropping daily, and smart sellers are quietly stacking profits.</p>
<p>If you understand how SEO landing pages actually work today, you’re not guessing. Instead, you're building assets that pull traffic and convert.</p>
<p>This guide walks you through how I researched, structured, built, and deployed a real SEO landing page tailored for affiliate marketers.</p>
<h3 id="heading-table-of-contents">Table of Contents</h3>
<ul>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a href="#heading-keyword-research">Keyword Research</a></p>
<ul>
<li><p><a href="#heading-supporting-keywords">Supporting Keywords</a></p>
</li>
<li><p><a href="#heading-how-did-i-get-those-keywords">How Did I Get Those Keywords?</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-what-to-do-after-keyword-research">What To Do After Keyword Research</a></p>
<ul>
<li><p><a href="#heading-lock-your-page-intent-critical">Lock Your Page Intent (Critical)</a></p>
</li>
<li><p><a href="#heading-group-your-keywords-clustering">Group Your Keywords (Clustering)</a></p>
</li>
<li><p><a href="#heading-analyze-the-serp-your-real-competitors">Analyze the SERP (Your Real Competitors)</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-create-your-seo-landing-page-blueprint">Create Your SEO Landing Page Blueprint</a></p>
<ul>
<li><p><a href="#heading-above-the-fold-conversion-zone">Above the Fold (Conversion Zone)</a></p>
</li>
<li><p><a href="#heading-main-content-ranking-and-conversion-zone">Main Content (Ranking and Conversion Zone)</a></p>
</li>
<li><p><a href="#heading-scalability-thinking">Scalability Thinking</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-how-i-built-the-project-development-process">How I Built the Project (Development Process)</a></p>
</li>
<li><p><a href="#heading-on-page-seo-setup">On-Page SEO Setup</a></p>
<ul>
<li><p><a href="#heading-project-structure">Project Structure</a></p>
</li>
<li><p><a href="#heading-seo-metadata-setup">SEO + Metadata Setup</a></p>
</li>
<li><p><a href="#heading-structured-data-schema-markup">Structured Data (Schema Markup)</a></p>
</li>
<li><p><a href="#heading-hero-section-above-the-fold-seo">Hero Section (Above-the-Fold SEO)</a></p>
</li>
<li><p><a href="#heading-value-section-keyword-reinforcement">Value Section (Keyword Reinforcement)</a></p>
</li>
<li><p><a href="#heading-benefits-grid-scannable-seo-content">Benefits Grid (Scannable SEO Content)</a></p>
</li>
<li><p><a href="#heading-product-section-conversion-affiliate-seo">Product Section (Conversion + Affiliate SEO)</a></p>
</li>
<li><p><a href="#heading-comparison-table-high-intent-seo-content">Comparison Table (High-Intent SEO Content)</a></p>
</li>
<li><p><a href="#heading-review-section">Review Section</a></p>
</li>
<li><p><a href="#heading-faq-section-search-expansion">FAQ Section (Search Expansion)</a></p>
</li>
<li><p><a href="#heading-cta-section-conversion-signal">CTA Section (Conversion Signal)</a></p>
</li>
<li><p><a href="#heading-footer">Footer</a></p>
</li>
<li><p><a href="#heading-javascript-ux-enhancements">JavaScript UX Enhancements</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-deployment-using-netlify">Deployment (Using Netlify)</a></p>
<ul>
<li><p><a href="#heading-why-netlify">Why Netlify?</a></p>
</li>
<li><p><a href="#heading-step-1-push-your-project-to-github">Step 1 — Push Your Project to GitHub</a></p>
</li>
<li><p><a href="#heading-enable-https">Enable HTTPS</a></p>
</li>
<li><p><a href="#heading-publish-and-index">Publish and Index</a></p>
</li>
<li><p><a href="#heading-post-publish-this-is-where-ranking-happens">Post-Publish (This Is Where Ranking Happens)</a></p>
</li>
<li><p><a href="#heading-turn-this-into-a-system-advanced-move">Turn This Into a System (Advanced Move)</a></p>
</li>
<li><p><a href="#heading-simple-reality-check">Simple Reality Check</a></p>
</li>
<li><p><a href="#heading-final-result">Final Result</a></p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before following this guide, you should have:</p>
<ul>
<li><p>Basic understanding of how websites work (HTML, CSS and JavaScript basics is helpful)</p>
</li>
<li><p>A GitHub account (for deployment)</p>
</li>
<li><p>A Netlify account (free)</p>
</li>
<li><p>Basic understanding of SEO (keywords, search intent)</p>
</li>
<li><p>A niche or product idea to build your landing page around</p>
</li>
</ul>
<p>Optional but helpful:</p>
<ul>
<li>Familiarity with tools like Google Keyword Planner, Ahrefs, or Semrush</li>
</ul>
<h2 id="heading-keyword-research">Keyword Research</h2>
<p>First, get your primary keyword. The primary keyword for the purpose of this article is "<strong>eco-friendly running shoes</strong>".</p>
<h3 id="heading-supporting-keywords">Supporting Keywords:</h3>
<ul>
<li><p>eco friendly running shoes for men</p>
</li>
<li><p>sustainable running shoes brands</p>
</li>
<li><p>biodegradable running shoes</p>
</li>
<li><p>vegan running shoes</p>
</li>
<li><p>best recycled material sneakers</p>
</li>
<li><p>eco running shoes review</p>
</li>
<li><p>affordable eco-friendly sneakers</p>
</li>
<li><p>lightweight sustainable running shoes</p>
</li>
</ul>
<h4 id="heading-how-did-i-get-those-keywords">How Did I Get Those Keywords?</h4>
<p>Well, by using tool-based research. I simulated the process with <a href="https://business.google.com/en-all/google-ads/">Google Keyword Planner</a>. There are also other tools like Semrush and Ahrefs you can use for this purpose.</p>
<h4 id="heading-process">Process</h4>
<ul>
<li><p>Entered: best <em>eco-friendly running shoes</em></p>
</li>
<li><p>Filtered for:</p>
<ul>
<li><p>Buyer intent (words like “best”, “buy”, “review”)</p>
</li>
<li><p>Medium/low competition</p>
</li>
<li><p>Long-tail phrases</p>
</li>
</ul>
</li>
</ul>
<h4 id="heading-serp-intent-research">SERP Intent Research</h4>
<p>Next, I opened my browser and searched: best eco-friendly running shoes</p>
<p>Then I analyzed:</p>
<ul>
<li><p>Are results blog posts or product pages?</p>
</li>
<li><p>Do they compare products?</p>
</li>
<li><p>Are there buying guides?</p>
</li>
</ul>
<p>I found a variety of list-style articles (listicles), product comparisons pages, affiliate-style landing pages, and strong CTAs like "Buy now” or “Check price”.</p>
<p>This tells us users want curated recommendations plus a clear path to purchase.</p>
<h4 id="heading-keyword-selection-logic">Keyword Selection Logic</h4>
<p>I picked the primary keyword because:</p>
<ul>
<li><p>It shows clear buying intent</p>
</li>
<li><p>It’s long-tail (easier to rank)</p>
</li>
<li><p>It matches a conversion-focused landing page</p>
</li>
</ul>
<h2 id="heading-what-to-do-after-keyword-research">What To Do After Keyword Research</h2>
<p>Once you have your keywords, don’t jump into writing yet. Do this:</p>
<h3 id="heading-lock-your-page-intent-critical">Lock Your Page Intent (Critical)</h3>
<p>Before writing anything, decide your page goal: Is it to sell or educate?</p>
<p>The four types of search intent are commercial, transactional, informational, and navigational.</p>
<p>For this keyword – <em>best eco-friendly running shoes</em> – the <strong>primary Intent</strong> is transactional (buying) while the <strong>secondary</strong> <strong>intent</strong> is informational.</p>
<p>So your page needs to help users choose the right product and guide them toward making a purchase.</p>
<p>If your intent is unclear, your page will struggle to rank.</p>
<h3 id="heading-group-your-keywords-clustering">Group Your Keywords (Clustering)</h3>
<p>Instead of treating keywords separately, organize them into groups.</p>
<p>Let's look at an example:</p>
<p><strong>Primary Keyword</strong></p>
<ul>
<li>best eco-friendly running shoes</li>
</ul>
<p><strong>Cluster 1 – Buyer Intent</strong></p>
<ul>
<li><p>eco friendly running shoes for men</p>
</li>
<li><p>best sustainable running shoes</p>
</li>
<li><p>affordable eco-friendly sneakers</p>
</li>
</ul>
<p><strong>Cluster 2 – Informational</strong></p>
<ul>
<li><p>what are eco-friendly running shoes</p>
</li>
<li><p>benefits of sustainable shoes</p>
</li>
<li><p>how eco shoes are made</p>
</li>
</ul>
<p>This matters because Google ranks pages based on topics, not just individual keywords.</p>
<h3 id="heading-analyze-the-serp-your-real-competitors">Analyze the SERP (Your Real Competitors)</h3>
<p>Search your keyword again and study the top 3 – 5 results.</p>
<p>Look at page structure, content length, and the sections included.</p>
<p>You should also check for patterns:</p>
<ul>
<li><p>Do they list multiple products?</p>
</li>
<li><p>Do they include comparison tables?</p>
</li>
<li><p>Are there FAQs?</p>
</li>
<li><p>Do they use images or videos?</p>
</li>
</ul>
<p>Your goal is not to copy but to build something better structured and more useful.</p>
<h2 id="heading-create-your-seo-landing-page-blueprint">Create Your SEO Landing Page Blueprint</h2>
<p>Now turn your research into a clear structure.</p>
<p>Your page should look like this:</p>
<h3 id="heading-above-the-fold-conversion-zone">Above the Fold (Conversion Zone)</h3>
<p>This is what users see first.</p>
<ul>
<li><p>H1: Best Eco-Friendly Running Shoes</p>
</li>
<li><p>Short sub-headline (value-focused)</p>
</li>
<li><p>Strong CTA (<em>for example, Shop Now, View Top Picks</em>)</p>
</li>
</ul>
<h3 id="heading-main-content-ranking-and-conversion-zone">Main Content (Ranking and Conversion Zone)</h3>
<p>This is where SEO and conversions happen together.</p>
<p>You should include:</p>
<ol>
<li><p><strong>Top Products Section</strong>: List at least 5 recommended shoes</p>
</li>
<li><p><strong>Benefits of Eco-Friendly Shoes</strong>: Educate users briefly</p>
</li>
<li><p><strong>Comparison Table</strong>: Help users quickly decide</p>
</li>
<li><p><strong>Product Reviews / Social Proof</strong>: Build trust</p>
</li>
<li><p><strong>FAQ Section</strong>: Answer common questions and capture extra search traffic.</p>
</li>
</ol>
<h3 id="heading-scalability-thinking">Scalability Thinking</h3>
<p>If your goal is to build multiple SEO pages, you need a system — not random pages. I designed this landing page to be scalable.</p>
<p>In practice, scalability means a few different things.</p>
<p>First, you'll need to have a reusable layout. Instead of designing every page from scratch, create one high-converting template (headline, product list, comparison table, FAQs). Then reuse it across different keywords.</p>
<p>Second, you'll need to understand content swapping. This means you only change the keyword, product list, images, and supporting content.</p>
<p>For example:</p>
<ul>
<li><p>Page 1: Best eco-friendly running shoes</p>
</li>
<li><p>Page 2: Best vegan running shoes</p>
</li>
<li><p>Page 3: Best sustainable gym wear</p>
</li>
</ul>
<p>You'll also want to make sure your site has a consistent structure as this results in faster ranking. Google understands your site better when pages follow a clear pattern.</p>
<p>You should also understand the advantages of internal linking. When you connect these pages together, you build topical authority, which improves rankings across all pages.</p>
<p>The goal is simple: build once, scale many times.</p>
<h2 id="heading-how-i-built-the-project-development-process">How I Built the Project (Development Process)</h2>
<p>This <a href="https://ecorunningshoes.netlify.app/">sample landing page</a> is designed for affiliate marketers.</p>
<p>Note that the brand names, values, images, and prices used on this page are fictitious and are for demonstration purposes only.</p>
<p>To build this page, I used HTML, CSS, and JavaScript.</p>
<p>My focus areas were:</p>
<ul>
<li><p>That it had a clean and structured layout</p>
</li>
<li><p>That is was fast loading and scored well on performance</p>
</li>
<li><p>That it had a mobile-friendly design</p>
</li>
</ul>
<h2 id="heading-on-page-seo-setup">On-Page SEO Setup</h2>
<p>Here’s a real example of how I built this SEO landing page:</p>
<h3 id="heading-project-structure">Project Structure</h3>
<pre><code class="language-markdown">ECO-PREMIUM/
├── index.html
├── styles.css
├── script.js
├── README.md
└── assets/
    └── shoes/
        ├── allbirds.jpg
        ├── reebok.jpg
        ├── brooks.jpg
        ├── veja.jpg
        ├── adidas.jpg
        ├── nike.jpg
        ├── hero-bg.jpg
</code></pre>
<h3 id="heading-seo-metadata-setup">SEO + Metadata Setup</h3>
<p>This is where SEO starts. Before design, I made sure Google understands the page instantly.</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;

&lt;title&gt;Best Eco-Friendly Running Shoes (2026 Guide)&lt;/title&gt;

&lt;meta name="description" content="Discover the best eco-friendly running shoes for men and women. Compare sustainable running shoes brands, reviews, and find affordable eco sneakers."&gt;

&lt;link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"&gt;

&lt;link rel="stylesheet" href="styles.css"&gt;

&lt;link rel="preload" as="image" href="assets/shoes/allbirds.jpg"&gt;
</code></pre>
<h4 id="heading-why-this-matters-seo-strategy">Why This Matters (SEO Strategy)</h4>
<ul>
<li><p>The <strong>viewport tag</strong> makes sure the page looks good on mobile, which is important for rankings.</p>
</li>
<li><p>The <strong>title</strong> combines the main keyword with a year like “2026” to keep it fresh and relevant in search.</p>
</li>
<li><p>The <strong>meta description</strong> briefly explains what users will get and encourages clicks.</p>
</li>
<li><p>The <strong>Font Awesome stylesheet</strong> adds scalable icons for better UI and trust signals.</p>
</li>
<li><p>The <strong>styles.css file</strong> controls the site’s design, ensuring a clean, responsive layout that supports user experience and engagement.</p>
</li>
<li><p>And <strong>preloading key images</strong> helps the page load faster, improving performance and SEO.</p>
</li>
</ul>
<h3 id="heading-structured-data-schema-markup">Structured Data (Schema Markup)</h3>
<pre><code class="language-html">&lt;script type="application/ld+json"&gt;
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What are eco-friendly running shoes?",
"acceptedAnswer": {"@type": "Answer","text": "They are shoes made from sustainable materials."}
}
]
}
&lt;/script&gt;
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>It enables rich snippets in Google, improves click-through rates, and strengthens topical authority.</li>
</ul>
<h3 id="heading-hero-section-above-the-fold-seo">Hero Section (Above-the-Fold SEO)</h3>
<p>This is the most important section for both users and search engines.</p>
<pre><code class="language-html">&lt;header class="hero"&gt;
  &lt;div class="hero-overlay"&gt;&lt;/div&gt;

  &lt;div class="hero-content"&gt;
    &lt;h1&gt;Best Eco-Friendly Running Shoes&lt;/h1&gt;
    &lt;p&gt;High-performance. Sustainable. Built for runners who care.&lt;/p&gt;
    &lt;a href="#products" class="btn"&gt;Explore Top Picks&lt;/a&gt;
  &lt;/div&gt;
&lt;/header&gt;
</code></pre>
<p>The styling:</p>
<pre><code class="language-css">.hero {
  background:url('assets/shoes/hero-bg.jpg') center/cover no-repeat;
  min-height:90vh;
  display:flex;
  justify-content:center;
  align-items:center;
  text-align:center;
}

.hero-overlay {
  position:absolute;
  inset:0;
  background:rgba(0,0,0,0.6);
}
</code></pre>
<h5 id="heading-the-ui-view">The UI view:</h5>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/1b58dc21-d469-4ec0-b7bd-8fff6437d4ef.png" alt="1b58dc21-d469-4ec0-b7bd-8fff6437d4ef" style="display:block;margin:0 auto" width="1346" height="574" loading="lazy">

<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>The H1 targets the primary keyword, while strong visual engagement helps reduce bounce rate, and a clear CTA improves user interaction signals.</li>
</ul>
<h3 id="heading-value-section-keyword-reinforcement">Value Section (Keyword Reinforcement)</h3>
<p>This section strengthens semantic SEO relevance.</p>
<pre><code class="language-html">&lt;section class="value"&gt;
  &lt;h2&gt;Premium Sustainable Running Experience&lt;/h2&gt;
  &lt;p&gt;
    Discover eco friendly running shoes for men and women designed with recycled materials.
  &lt;/p&gt;
&lt;/section&gt;
</code></pre>
<pre><code class="language-css">section {
  padding:80px 20px;
  max-width:1100px;
  margin:auto;
}
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>It supports keyword variations, improves topical depth, and helps Google better understand search intent.</li>
</ul>
<h3 id="heading-benefits-grid-scannable-seo-content">Benefits Grid (Scannable SEO Content)</h3>
<p>Google favors structured, easy-to-scan content.</p>
<pre><code class="language-html">&lt;section class="benefits"&gt;
  &lt;h2&gt;Why Choose Eco Running Shoes&lt;/h2&gt;
  &lt;div class="grid"&gt;
    &lt;div&gt;Sustainable materials&lt;/div&gt;
    &lt;div&gt;Lower carbon footprint&lt;/div&gt;
    &lt;div&gt;Lightweight design&lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;
</code></pre>
<p>The styling:</p>
<pre><code class="language-css">.grid {
  display:grid;
  grid-template-columns:repeat(auto-fit,minmax(200px,1fr));
  gap:20px;
}
</code></pre>
<h5 id="heading-the-ui-view">The UI view:</h5>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/91c162ce-b8cd-47a1-8215-04e9d37e161d.png" alt="91c162ce-b8cd-47a1-8215-04e9d37e161d" style="display:block;margin:0 auto" width="1345" height="630" loading="lazy">

<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>It supports keyword variations, improves topical depth, and helps Google better understand search intent.</li>
</ul>
<h3 id="heading-product-section-conversion-affiliate-seo">Product Section (Conversion + Affiliate SEO)</h3>
<p>This is where traffic turns into revenue.</p>
<pre><code class="language-html">&lt;!-- PRODUCTS --&gt;
&lt;section id="products" class="products"&gt;
  &lt;h2&gt;Top Picks&lt;/h2&gt;

  &lt;div class="cards"&gt;

    &lt;div class="card"&gt;
      &lt;img src="assets/shoes/allbirds.jpg" alt="Allbirds Tree     Dasher eco-friendly running shoes made from sustainable materials" loading="lazy"&gt;
      &lt;h3&gt;Allbirds Tree Dasher&lt;/h3&gt;
      &lt;p&gt;Lightweight sustainable running shoes.&lt;/p&gt;
      &lt;a href="https://example.com/allbirds-affiliate" target="_blank" class="btn"&gt;Check Price&lt;/a&gt;
    &lt;/div&gt;
 &lt;/div&gt;
&lt;/section&gt;
</code></pre>
<p>The styling:</p>
<pre><code class="language-css">.cards {
  display:grid;
  grid-template-columns:repeat(3,1fr);
  gap:30px;
}

.card {
  background:#111;
  padding:20px;
  border-radius:20px;
}
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>Lazy loading improves performance, a well-structured product layout drives affiliate conversions, and animations enhance user engagement.</li>
</ul>
<p>The UI view:</p>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/55199b5d-86fe-42a7-a7d5-22c9ef29c907.png" alt="55199b5d-86fe-42a7-a7d5-22c9ef29c907" style="display:block;margin:0 auto" width="1343" height="504" loading="lazy">

<h4 id="heading-avoid-this-common-mistake-for-image-alt-text">Avoid this common mistake for image alt text:</h4>
<pre><code class="language-html">alt="eco shoes eco friendly running shoes best eco shoes cheap eco shoes"
</code></pre>
<p>That hurts SEO more than it helps.</p>
<p>This is bad because:</p>
<ul>
<li><p>It’s keyword stuffing (Google may treat this as spam)</p>
</li>
<li><p>It provides no real description of the image</p>
</li>
<li><p>It creates a poor experience for screen readers (accessibility issue)</p>
</li>
</ul>
<p>What Google prefers:</p>
<p>Alt text should describe the image naturally while including the keyword where relevant.</p>
<h4 id="heading-pro-tip-for-your-whole-site">Pro Tip (for your whole site)</h4>
<p>Use this formula for every product image alt text:</p>
<p><strong>[Product Name] + [Main Feature] + [Keyword]</strong></p>
<p>Example:</p>
<ul>
<li>“Nike Air Zoom eco-friendly running shoes with recycled materials”</li>
</ul>
<p>This improves: SEO relevance, Accessibility, and User experience</p>
<h3 id="heading-comparison-table-high-intent-seo-content">Comparison Table (High-Intent SEO Content)</h3>
<p>This targets buyers ready to decide.</p>
<pre><code class="language-html">&lt;section class="comparison"&gt;
  &lt;h2&gt;Comparison Table&lt;/h2&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;th&gt;Brand&lt;/th&gt;&lt;th&gt;Weight&lt;/th&gt;&lt;th&gt;Material&lt;/th&gt;                   &lt;th&gt;Durability&lt;/th&gt;
      &lt;th&gt;Comfort&lt;/th&gt;&lt;th&gt;Eco Score&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;th&gt;Best For&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Allbirds&lt;/td&gt;&lt;td&gt;Light&lt;/td&gt;&lt;td&gt;Eucalyptus, fibre, sugarcane&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;
      &lt;td&gt;Very Good&lt;/td&gt;&lt;td&gt;8/10&lt;/td&gt;&lt;td&gt;$125&lt;/td&gt;&lt;td&gt;Daily runs&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/section&gt;
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>It captures comparison keywords, improves time on page, and increases visibility for "product vs product" searches.</li>
</ul>
<p>The UI view:</p>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/47c40801-7dc0-41c7-8959-169e4c8f12b9.png" alt="47c40801-7dc0-41c7-8959-169e4c8f12b9" style="display:block;margin:0 auto" width="1249" height="509" loading="lazy">

<h3 id="heading-review-section">Review Section</h3>
<pre><code class="language-html">&lt;section class="reviews"&gt;
  &lt;h2&gt;Trusted by Runners&lt;/h2&gt;
  &lt;p&gt;⭐⭐⭐⭐⭐ "Right place to buy the best eco-friendly sneakers"&lt;/p&gt;
  &lt;p&gt;⭐⭐⭐⭐⭐ "Brands they sell are lightweight and durable"&lt;/p&gt;
  &lt;p&gt;⭐⭐⭐⭐ "My order arrived on time, I like their timing!"&lt;/p&gt;
&lt;/section&gt;
</code></pre>
<h4 id="heading-why-this-matters">Why this matters</h4>
<ul>
<li>It builds trust and makes people more likely to click and buy. It also helps SEO by adding real user language and improving engagement.</li>
</ul>
<h3 id="heading-faq-section-search-expansion">FAQ Section (Search Expansion)</h3>
<pre><code class="language-html">&lt;section class="faq"&gt;
  &lt;h2&gt;FAQs&lt;/h2&gt;

  &lt;details&gt;
    &lt;summary&gt;What are eco-friendly running shoes?&lt;/summary&gt;
    &lt;p&gt;Eco-friendly running shoes are made using sustainable or recycled materials like organic cotton, eucalyptus fiber, and recycled plastics to reduce environmental impact.&lt;/p&gt;
  &lt;/details&gt;
&lt;/section&gt;
</code></pre>
<p>The UI view:</p>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/c17e8bdf-603c-4935-9cfa-b0348585d0f5.png" alt="c17e8bdf-603c-4935-9cfa-b0348585d0f5" style="display:block;margin:0 auto" width="1055" height="626" loading="lazy">

<h4 id="heading-why-this-matters">Why this matters</h4>
<ul>
<li>It targets long-tail keywords, supports featured snippets, and reinforces schema relevance.</li>
</ul>
<h3 id="heading-cta-section-conversion-signal">CTA Section (Conversion Signal)</h3>
<pre><code class="language-html">&lt;section class="cta"&gt;
  &lt;h2&gt;Start Running Sustainably&lt;/h2&gt;
  &lt;a href="#products" class="btn"&gt;Shop Now&lt;/a&gt;
&lt;/section&gt;
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>It encourages user action, improves engagement metrics, and signals to Google that the content is useful and valuable.</li>
</ul>
<h3 id="heading-footer">Footer</h3>
<pre><code class="language-html">&lt;!-- FOOTER --&gt;
&lt;footer class="footer"&gt;

  &lt;div class="footer-top"&gt;

    &lt;div class="footer-brand"&gt;
      &lt;img src="assets/logo.png" alt="Logo" class="logo"&gt;
      &lt;p&gt;Trusted eco-friendly product reviews.&lt;/p&gt;
    &lt;/div&gt;
  &lt;div class="footer-bottom"&gt;
    &lt;p&gt;© 2026 Eco Running Guide&lt;/p&gt;
  &lt;/div&gt;

&lt;/footer&gt;
</code></pre>
<p>The UI view:</p>
<img src="https://cdn.hashnode.com/uploads/covers/647d7b660f441a49aa878a9e/2afd2119-b3f8-4513-8989-353e37491d0e.png" alt="2afd2119-b3f8-4513-8989-353e37491d0e" style="display:block;margin:0 auto" width="1337" height="527" loading="lazy">

<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>The footer builds trust, provides important legal and navigation links, and supports SEO by reinforcing site credibility.</li>
</ul>
<h3 id="heading-javascript-ux-enhancements">JavaScript UX Enhancements</h3>
<pre><code class="language-html">&lt;html&gt;
.
.
.
&lt;body&gt;
.
.
.
&lt;script src="script.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h5 id="heading-javascript-code-snippet"><strong>JavaScript code snippet:</strong></h5>
<pre><code class="language-javascript">// Smooth scroll
document.querySelectorAll('a[href^="#"]').forEach(anchor=&gt;{
  anchor.addEventListener("click",function(e){
    e.preventDefault();
    document.querySelector(this.getAttribute("href"))
    .scrollIntoView({behavior:"smooth"});
  });
});

// Fade-in cards
const cards = document.querySelectorAll('.card');
cards.forEach(card=&gt;{
  card.style.opacity = 0;
  card.style.transform = "translateY(20px)";
});

window.addEventListener('scroll', ()=&gt;{
  cards.forEach(card=&gt;{
    const rect = card.getBoundingClientRect();
    if(rect.top &lt; window.innerHeight - 50){
      card.style.opacity = 1;
      card.style.transform = "translateY(0)";
      card.style.transition = "0.5s";
    }
  });
});
</code></pre>
<h4 id="heading-why-this-matters">Why This Matters</h4>
<ul>
<li>The script link ensures interactive features (like smooth scrolling and animations) load properly, improving user experience and engagement.</li>
</ul>
<p>Note that the above code snippets are part of the main code file. You can copy or clone the project code files from this <a href="https://github.com/nuelcas/seo-landing-page.git">repository</a>.</p>
<h2 id="heading-deployment-using-netlify">Deployment (Using Netlify)</h2>
<h3 id="heading-why-netlify">Why Netlify?</h3>
<p>Netlify is one of the easiest and fastest ways to deploy modern websites. It’s widely used by developers because it has:</p>
<ul>
<li><p>Free hosting for small to medium projects</p>
</li>
<li><p>Automatic deployment from GitHub (no manual uploads)</p>
</li>
<li><p>A fast global CDN (your site loads quickly anywhere)</p>
</li>
<li><p>Built-in HTTPS (SSL security included)</p>
</li>
<li><p>Simple custom domain setup</p>
</li>
<li><p>It's perfect for static sites (HTML, CSS, JS landing pages)</p>
</li>
</ul>
<p>In simple terms, Netlify removes the stress of server setup and lets you focus on building and improving your site.</p>
<h3 id="heading-step-1-push-your-project-to-github">Step 1 — Push Your Project to GitHub</h3>
<p>Before anything, make sure your landing page project is already uploaded to GitHub.</p>
<h4 id="heading-connect-to-netlify">Connect to Netlify</h4>
<ol>
<li><p>Go to <a href="https://app.netlify.com/">Netlify</a> and sign up or log in.</p>
</li>
<li><p>Click “Add new site”</p>
</li>
<li><p>Select “Import an existing project”</p>
</li>
<li><p>Choose GitHub</p>
</li>
<li><p>Authorize Netlify to access your repositories</p>
</li>
</ol>
<h4 id="heading-select-your-repository">Select Your Repository</h4>
<p>Then find your project repo (for example,<code>seo-landing-landing</code>) and click on it. Netlify will automatically detect it as a static site.</p>
<h4 id="heading-deploy-the-site">Deploy the Site</h4>
<p>Now click “Deploy site”. Netlify will build and host your project. After a few seconds, you’ll get a live URL.</p>
<p>Example:</p>
<pre><code class="language-plaintext">https://ecorunningshoes.netlify.app/
</code></pre>
<h4 id="heading-adding-a-custom-domain">Adding a Custom Domain</h4>
<p>To make your site look professional (instead of using <code>netlify.app</code>), you can connect your own domain like:</p>
<pre><code class="language-plaintext">www.yourdomain.com
</code></pre>
<h5 id="heading-steps"><strong>Steps</strong>:</h5>
<ol>
<li><p>Go to your Netlify dashboard</p>
</li>
<li><p>Open your deployed site</p>
</li>
<li><p>Click “Domain settings”</p>
</li>
<li><p>Select “Add custom domain”</p>
</li>
<li><p>Enter your domain name (e.g. <code>ecorunshoes.com</code>)</p>
</li>
<li><p>Click "verify"</p>
</li>
</ol>
<h4 id="heading-configure-dns-important">Configure DNS (Important)</h4>
<p>After adding your domain, go to your domain provider (for example, Namecheap, GoDaddy) and update the DNS records as Netlify instructs:</p>
<ul>
<li><p>Add CNAME record pointing to Netlify</p>
</li>
<li><p>Or use Netlify DNS (recommended) for easier setup</p>
</li>
</ul>
<h3 id="heading-enable-https">Enable HTTPS</h3>
<p>Once the domain is connected, Netlify automatically issues a free SSL certificate. Your site becomes secure:</p>
<pre><code class="language-plaintext">https://yourdomain.com
</code></pre>
<h3 id="heading-publish-and-index">Publish and Index</h3>
<p>After deployment, make sure to submit the URL to <a href="https://search.google.com/search-console/welcome">Google Search Console</a> and request indexing.</p>
<h3 id="heading-post-publish-this-is-where-ranking-happens">Post-Publish (This Is Where Ranking Happens)</h3>
<p>Most pages don’t rank because people stop at publishing.</p>
<p>There are a few more things you should do before you're done:</p>
<h4 id="heading-build-backlinks">Build backlinks</h4>
<ul>
<li><p>Add guest posts</p>
</li>
<li><p>Blog mentions</p>
</li>
<li><p>Directories</p>
</li>
</ul>
<h4 id="heading-update-content">Update content</h4>
<ul>
<li><p>Add new products</p>
</li>
<li><p>Improve sections</p>
</li>
<li><p>Refresh FAQs</p>
</li>
</ul>
<h3 id="heading-turn-this-into-a-system-advanced-move">Turn This Into a System (Advanced Move)</h3>
<p>Don’t just build ONE page. Build a cluster system:</p>
<p>The main page will focus on "best eco-friendly running shoes".</p>
<p>The Supporting pages:</p>
<ul>
<li><p>best vegan running shoes</p>
</li>
<li><p>eco-friendly gym wear</p>
</li>
<li><p>sustainable shoe brands</p>
</li>
</ul>
<p>Link them together so authority increases and rankings grow faster.</p>
<h3 id="heading-simple-reality-check">Simple Reality Check</h3>
<p>Keyword research is only <strong>20% of SEO</strong>.</p>
<p>The other 80% is:</p>
<ul>
<li><p>Structure</p>
</li>
<li><p>Intent matching</p>
</li>
<li><p>Content depth</p>
</li>
<li><p>Authority (backlinks)</p>
</li>
</ul>
<p>When these work together, your page doesn’t just rank — it sells.</p>
<h2 id="heading-final-result">Final Result</h2>
<p>You now have a live SEO landing page that's hosted for free on Netlify and automatically updated from GitHub, as well as a professional custom domain (optional but recommended).</p>
<p>A high-ranking SEO landing page works best when strategy, structure, and intent are properly aligned. Good keyword research, SERP analysis, and a clear page layout all contribute to both visibility and conversions.</p>
<p>When combined with solid on-page SEO and proper deployment, your landing page becomes a reliable asset that can attract traffic and deliver results over time. Success in SEO also depends on consistent updates and ongoing optimization after publishing.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Structure Your README File – README Template Example ]]>
                </title>
                <description>
                    <![CDATA[ As a developer who aspires to be a founder, building your first startup can be filled with excitement and ideas. The worst thing that could happen to you is jumping straight into the coding part. I was in this situation and the last thing on my mind ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-structure-your-readme-file/</link>
                <guid isPermaLink="false">690e02f392fcbf371b6e1b9d</guid>
                
                    <category>
                        <![CDATA[ Collaboration ]]>
                    </category>
                
                    <category>
                        <![CDATA[ GitHub ]]>
                    </category>
                
                    <category>
                        <![CDATA[ startup ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Fri, 07 Nov 2025 14:32:19 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762523233143/4555ff83-b390-4cb2-b6de-acea129de4b1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>As a developer who aspires to be a founder, building your first startup can be filled with excitement and ideas. The worst thing that could happen to you is jumping straight into the coding part. I was in this situation and the last thing on my mind was writing a README file.</p>
<p>I thought, <em>“I’ll add it later.”</em> But “later” never came.</p>
<p>Weeks turned into months, and my once-simple idea turned into chaos. A developer who joined my project had no idea how to set it up. Even I, the founder, started forgetting why I structured certain parts of the app the way I did.</p>
<p>What was supposed to be a few months of development stretched to nearly a year. All because I ignored one small file: <strong>the README.</strong></p>
<p>In this article, you’ll learn how to structure your README file to show all the important information about your project. You can see what it’ll look like here: <a target="_blank" href="https://github.com/nuelcas/mybrandname.git">MybrandName repo</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-the-readme-file-is-not-just-a-formality">The README File is Not Just a Formality</a></p>
<ul>
<li><a class="post-section-overview" href="#heading-readme-structure">README Structure</a></li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-mybrandname-ai-branding-assistant">MyBrandName — AI Branding Assistant</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-features">Features</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-tech-stack">Tech Stack</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-quick-start">Quick Start</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-installations">Installations</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-repository-structure">Repository Structure</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-architecture-overview">Architecture Overview</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-example-api-endpoints">Example API Endpoints</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-authentication-supabase">Authentication (Supabase)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-environment-variables">Environment Variables</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-testing">Testing</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-continuous-integration-ci">Continuous Integration (CI)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-versioning-amp-changelog">Versioning &amp; Changelog</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-contributing">Contributing</a></p>
<ul>
<li><a class="post-section-overview" href="#heading-code-of-conduct">Code of Conduct</a></li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-deployment">Deployment</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-license">License</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-github-repository">The GitHub Repository</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-developer-checklist">Developer Checklist</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-common-pitfalls-amp-how-to-avoid-them-beginner-friendly">Common Pitfalls &amp; How to Avoid Them (Beginner-Friendly)</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-problem-hardcoding-api-keys">Problem: Hardcoding API Keys</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-problem-no-quick-start-section">Problem: No Quick Start Section</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-problem-missing-example-requests-or-screenshots">Problem: Missing Example Requests or Screenshots</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-problem-confusing-folder-structure">Problem: Confusing Folder Structure</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-problem-forgetting-to-version-your-project">Problem: Forgetting to Version Your Project</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-problem-no-testing-before-deployment">Problem: No Testing Before Deployment</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-what-you-can-learn-from-this">💡 What You Can Learn from This</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-final-words">Final Words</a></p>
</li>
</ul>
<h2 id="heading-the-readme-file-is-not-just-a-formality">The README File is Not Just a Formality</h2>
<p>Many beginners see the README as optional—something you add just before submitting your GitHub repo. But that’s isn’t the right mindset.</p>
<p>Your README is your project’s map. It tells any developer (including your future self) where to start, how to set up the environment, and how everything connects. It saves time, reduces frustration, and turns a pile of code into a usable, understandable project.</p>
<p>If someone can clone your repository and get it running in under 10 minutes, your README did its job!</p>
<h3 id="heading-readme-structure">README Structure</h3>
<p>Your README acts like the user manual for any developer who clones your repository. It should guide a developer to:</p>
<ul>
<li><p>Clone the repo.</p>
</li>
<li><p>Install dependencies.</p>
</li>
<li><p>Configure environment variables.</p>
</li>
<li><p>Run both backend and frontend successfully.</p>
</li>
<li><p>Understand how the system works.</p>
</li>
</ul>
<p>Let me walk you through a sample README from a project called <strong>MyBrandName</strong>.</p>
<p>Here’s what the README looks like: <a target="_blank" href="https://github.com/nuelcas/mybrandname">https://github.com/nuelcas/mybrandname</a></p>
<h2 id="heading-mybrandname-ai-branding-assistant">MyBrandName — AI Branding Assistant</h2>
<p>MyBrandName is an AI-powered platform that helps startups create a complete brand identity—logos, stories, and marketing assets—in minutes.</p>
<h3 id="heading-features">Features</h3>
<ul>
<li><p><strong>AI-Powered Branding</strong> – Instantly generate logos, brand stories, and marketing assets using OpenAI.</p>
</li>
<li><p><strong>Authentication</strong> – Secure user login and registration powered by Supabase.</p>
</li>
<li><p><strong>Database</strong> – Supabase for storing users, brands, assets, and subscription data.</p>
</li>
<li><p><strong>Frontend</strong> – Responsive UI built with TypeScript, Vite, and TailwindCSS.</p>
</li>
<li><p><strong>Backend API</strong> – Node.js + Express handles AI generation, authentication, and data management.</p>
</li>
<li><p><strong>Subscription Management</strong> – Stripe integration for plan upgrades and payments.</p>
</li>
<li><p><strong>Continuous Integration (CI)</strong> – Automated testing and build workflows via GitHub Actions.</p>
</li>
<li><p><strong>Versioning &amp; Changelog</strong> – Semantic versioning with a clear project evolution record.</p>
</li>
<li><p><strong>Deployment Ready</strong> – Easily deploy frontend (Vercel) and backend (Render) with Supabase integration.</p>
</li>
</ul>
<h3 id="heading-tech-stack">Tech Stack</h3>
<ul>
<li><p><strong>Runtime:</strong> Node.js + Express.js.</p>
</li>
<li><p><strong>Language:</strong> TypeScript.</p>
</li>
<li><p><strong>Frontend:</strong> Vite + Tailwind CSS.</p>
</li>
<li><p><strong>Database &amp; Auth:</strong> Supabase (Database, Storage, Authentication).<br>  <strong>AI Service:</strong> OpenAI API (Logo, Story, and Content Generation).</p>
</li>
<li><p><strong>HTTP Client:</strong> Axios/Fetch API.</p>
</li>
<li><p><strong>CI/CD:</strong> GitHub Actions (Automated Testing &amp; Deployment).</p>
</li>
<li><p><strong>Hosting:</strong> Vercel (Frontend) + Render (Backend).</p>
</li>
</ul>
<h2 id="heading-quick-start">Quick Start</h2>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li><p><strong>Node.js 16+</strong></p>
</li>
<li><p><strong>Supabase project</strong> (for Authentication, Database, and Storage)</p>
</li>
<li><p><strong>OpenAI API key</strong> (for AI-powered logo and content generation)</p>
</li>
<li><p><strong>Stripe account</strong> (for subscription and payment handling)</p>
</li>
</ul>
<h3 id="heading-installations">Installations</h3>
<ol>
<li>Clone the repository</li>
</ol>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/nuelcas/mybrandname.git
</code></pre>
<ol start="2">
<li>Install Dependencies</li>
</ol>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> backend &amp;&amp; npm install
<span class="hljs-built_in">cd</span> ../frontend &amp;&amp; npm install
</code></pre>
<ol start="3">
<li>Environment setup</li>
</ol>
<pre><code class="lang-bash">cp backend/.env.example backend/.env
</code></pre>
<p>Update <code>.env</code> with your configuration:</p>
<ul>
<li><p>Supabase URL and API key</p>
</li>
<li><p>OpenAI API key</p>
</li>
<li><p>Stripe API key</p>
</li>
</ul>
<ol start="4">
<li>Development</li>
</ol>
<pre><code class="lang-bash"><span class="hljs-comment"># Run backend</span>
<span class="hljs-built_in">cd</span> backend &amp;&amp; npm run dev

<span class="hljs-comment"># Run frontend</span>
<span class="hljs-built_in">cd</span> frontend &amp;&amp; npm run dev
</code></pre>
<ol start="5">
<li>Production Build</li>
</ol>
<pre><code class="lang-bash">npm run build
npm start
</code></pre>
<p>Visit: <a target="_blank" href="http://localhost:5173">http://localhost:5173</a></p>
<h2 id="heading-repository-structure">Repository Structure</h2>
<pre><code class="lang-bash">/mybrandname
├── /frontend
│   ├── /src
│   │   ├── /components        <span class="hljs-comment"># UI Components (AuthForm, Navbar, etc.)</span>
│   │   ├── /pages             <span class="hljs-comment"># App pages (Home, Dashboard, Pricing)</span>
│   │   ├── /hooks             <span class="hljs-comment"># Custom React hooks (useAuth, useLogoGenerator)</span>
│   │   ├── /lib               <span class="hljs-comment"># Config files (Supabase, API client, constants)</span>
│   │   ├── /styles            <span class="hljs-comment"># Global and component styles</span>
│   │   ├── App.tsx            <span class="hljs-comment"># Main routing setup</span>
│   │   └── main.tsx           <span class="hljs-comment"># React entry point</span>
│   ├── public/                <span class="hljs-comment"># Public assets (icons, logos)</span>
│   ├── tailwind.config.ts     <span class="hljs-comment"># Configures Tailwind CSS settings</span>
│   ├── vite.config.ts         <span class="hljs-comment"># Contains build and development settings for the Vite bundler</span>
│   └── package.json           <span class="hljs-comment"># Lists frontend project dependencies, scripts, and metadata</span>
│
├── /backend
│   ├── /src
│   │   ├── /routes            <span class="hljs-comment"># Express routes (auth, brand, assets, subscription)</span>
│   │   ├── server.ts          <span class="hljs-comment"># Main Express server entry</span>
│   │   └── config/            <span class="hljs-comment"># Environment and DB configs</span>
│   └── package.json           <span class="hljs-comment"># Lists backend project dependencies, scripts, and metadata for Node.js</span>
│
└── README.md
</code></pre>
<h3 id="heading-architecture-overview">Architecture Overview</h3>
<p><strong>Frontend</strong></p>
<ul>
<li><p>Built with TypeScript + Vite + Tailwind CSS</p>
</li>
<li><p>Connects to Supabase for authentication, backend API for AI generation, and Stripe for payments</p>
</li>
</ul>
<p><strong>Backend</strong></p>
<ul>
<li><p>Built with Node.js + Express</p>
</li>
<li><p>Handles authentication, AI content generation, and database writes via Supabase</p>
</li>
</ul>
<p><strong>Supabase Tables</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Table</strong></td><td><strong>Purpose</strong></td></tr>
</thead>
<tbody>
<tr>
<td>users</td><td>Stores user accounts</td></tr>
<tr>
<td>brands</td><td>Saves generated brand info</td></tr>
<tr>
<td>assets</td><td>Links to stored images/files</td></tr>
<tr>
<td>subscriptions</td><td>Tracks plan and payment status</td></tr>
</tbody>
</table>
</div><h3 id="heading-example-api-endpoints">Example API Endpoints</h3>
<p><strong>Auth Routes</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Endpoint</strong></td><td><strong>Method</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td>/api/auth/signup</td><td>POST</td><td>Register new user</td></tr>
<tr>
<td>/api/auth/login</td><td>POST</td><td>Log in user</td></tr>
</tbody>
</table>
</div><p><strong>Branding Routes</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Endpoint</strong></td><td><strong>Method</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td>/api/brand/logo</td><td>POST</td><td>Generate AI-powered logo</td></tr>
</tbody>
</table>
</div><p>Example Request:</p>
<pre><code class="lang-bash">POST /api/brand/logo
{
  <span class="hljs-string">"brandName"</span>: <span class="hljs-string">"NovaTech"</span>,
  <span class="hljs-string">"industry"</span>: <span class="hljs-string">"Tech"</span>,
  <span class="hljs-string">"style"</span>: <span class="hljs-string">"Modern Minimal"</span>
}
</code></pre>
<p>Example Response:</p>
<pre><code class="lang-bash">{
  <span class="hljs-string">"logoUrl"</span>: <span class="hljs-string">"https://supabase.storage/novatech-logo.png"</span>,
  <span class="hljs-string">"palette"</span>: [<span class="hljs-string">"#121212"</span>, <span class="hljs-string">"#FF005C"</span>]
}
</code></pre>
<h3 id="heading-authentication-supabase">Authentication (Supabase)</h3>
<pre><code class="lang-bash">import { createClient } from <span class="hljs-string">'@supabase/supabase-js'</span>;

const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_KEY
);
</code></pre>
<h3 id="heading-environment-variables">Environment Variables</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Variable</strong></td><td><strong>Description</strong></td></tr>
</thead>
<tbody>
<tr>
<td>VITE_SUPABASE_URL</td><td>Supabase project URL</td></tr>
<tr>
<td>OPENAI_API_KEY</td><td>API key for AI generation</td></tr>
<tr>
<td>PORT</td><td>Backend port (default: 5000)</td></tr>
</tbody>
</table>
</div><h3 id="heading-testing">Testing</h3>
<p>Use Vitest/Jest for unit testing and Supertest for API routes.</p>
<pre><code class="lang-bash">npm run <span class="hljs-built_in">test</span>
</code></pre>
<h3 id="heading-continuous-integration-ci">Continuous Integration (CI)</h3>
<p>CI automatically runs tests when you push new code. This ensures your main branch always stays stable.</p>
<p>Example GitHub Action Workflow:</p>
<pre><code class="lang-bash">name: MyBrandName CI
on: [push, pull_request]
<span class="hljs-built_in">jobs</span>:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: |
          <span class="hljs-built_in">cd</span> backend &amp;&amp; npm ci &amp;&amp; npm run <span class="hljs-built_in">test</span>
          <span class="hljs-built_in">cd</span> ../frontend &amp;&amp; npm ci &amp;&amp; npm run build
</code></pre>
<p><strong>Tip:</strong> CI helps avoid “it works on my machine” problems.</p>
<h3 id="heading-versioning-amp-changelog">Versioning &amp; Changelog</h3>
<p>Keep a <a target="_blank" href="http://CHANGELOG.md"><code>CHANGELOG.md</code></a> file documenting updates.<br>Use <strong>Semantic Versioning (MAJOR.MINOR.PATCH)</strong>, for example,<br><code>1.1.0</code> → Added new features.</p>
<h2 id="heading-contributing">Contributing</h2>
<p>We welcome contributions from developers who want to improve <strong>MyBrandName</strong>!<br>Follow these steps to contribute effectively:</p>
<ul>
<li><p><strong>Fork the Repository</strong></p>
<ul>
<li>Click the <em>Fork</em> button on GitHub to create your own copy of the project.</li>
</ul>
</li>
<li><p><strong>Clone Your Fork</strong></p>
<ul>
<li>Run:</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">    git <span class="hljs-built_in">clone</span> https://github.com/nuelcas/mybrandname.git
</code></pre>
<ul>
<li><p><strong>Create a Feature Branch</strong></p>
<ul>
<li>Keep your changes organized:</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">    git checkout -b feat/your-feature-name
</code></pre>
<ul>
<li><p><strong>Set Up the Environment</strong></p>
<ul>
<li>Follow the setup instructions in the README to install dependencies and configure your <code>.env</code> files.</li>
</ul>
</li>
<li><p><strong>Follow Code Style and Formatting Rules</strong></p>
<ul>
<li>Ensure consistent formatting before committing:</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">    npm run lint
</code></pre>
<ul>
<li><p><strong>Use Clear Commit Messages</strong></p>
<ul>
<li><p>Follow the conventional commit style:</p>
<ul>
<li><p><code>feat:</code> – new feature</p>
</li>
<li><p><code>fix:</code> – bug fix</p>
</li>
<li><p><code>docs:</code> – documentation update</p>
</li>
<li><p><code>refactor:</code> – code restructuring</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Write or Update Tests</strong></p>
<ul>
<li><p>Use <code>Vitest</code> or <code>Jest</code> for unit testing and <code>Supertest</code> for API routes.</p>
</li>
<li><p>Run:</p>
</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">    npm run <span class="hljs-built_in">test</span>
</code></pre>
<ul>
<li><p><strong>Document Your Changes</strong></p>
<ul>
<li>Update <a target="_blank" href="http://README.md"><code>README.md</code></a>, <a target="_blank" href="http://CHANGELOG.md"><code>CHANGELOG.md</code></a>, or <a target="_blank" href="http://CONTRIBUTING.md"><code>CONTRIBUTING.md</code></a> if needed.</li>
</ul>
</li>
<li><p><strong>Submit a Pull Request (PR)</strong></p>
<ul>
<li><p>Push your branch and open a PR with:</p>
<ul>
<li><p>A short, clear description of your changes.</p>
</li>
<li><p>Any related issue numbers (for example, “Closes #12”).</p>
</li>
<li><p>Screenshots or example outputs (if applicable).</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Participate in Code Review</strong></p>
<ul>
<li>Respond to feedback, make improvements, and help maintain project quality.</li>
</ul>
</li>
</ul>
<h3 id="heading-code-of-conduct">Code of Conduct</h3>
<p>To maintain a positive and inclusive community, all contributors are expected to:</p>
<ul>
<li><p>Be respectful, kind, and patient when interacting with others.</p>
</li>
<li><p>Welcome feedback and engage in constructive discussions.</p>
</li>
<li><p>Avoid discriminatory or offensive language.</p>
</li>
<li><p>Focus on collaboration and problem-solving rather than criticism.</p>
</li>
<li><p>Credit other contributors where due.</p>
</li>
<li><p>Report any violations or concerns to the maintainers privately.</p>
</li>
</ul>
<p>Let’s work together to make <strong>MyBrandName</strong> a project where everyone feels valued and supported. 💙</p>
<h2 id="heading-deployment">Deployment</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Component</strong></td><td><strong>Platform</strong></td><td><strong>Notes</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Frontend</td><td>Vercel/Netlify</td><td>Add env variables</td></tr>
<tr>
<td>Backend</td><td>Render/Railway</td><td>Add Supabase &amp; AI keys</td></tr>
<tr>
<td>Database</td><td>Supabase</td><td>Auth + Storage + Database</td></tr>
</tbody>
</table>
</div><h3 id="heading-license">License</h3>
<p>This project is licensed under the MIT License—see the LICENSE file for details.</p>
<h3 id="heading-the-github-repository">The GitHub Repository</h3>
<p>You can clone the GitHub repo, edit and build your app from it: <a target="_blank" href="https://github.com/nuelcas/mybrandname.git">MybrandName repo.</a></p>
<h3 id="heading-developer-checklist"><strong>Developer Checklist</strong></h3>
<p>Think of this checklist as your <em>final review</em> before sharing your app publicly:</p>
<p><strong>1. Supabase Authentication is Working</strong></p>
<ul>
<li><p>Test your login and registration flow.</p>
</li>
<li><p>Try creating a new account and logging in.</p>
</li>
<li><p>Make sure the user’s data appears correctly in the Supabase “users” table.</p>
</li>
</ul>
<p><strong>2. AI Endpoints Return Proper Results</strong></p>
<ul>
<li><p>Test your backend endpoints for AI-powered features (for example, logo generation).</p>
</li>
<li><p>Use tools like <strong>Postman</strong> to send sample requests.</p>
</li>
<li><p>Confirm that Supabase stores the generated data or files correctly.</p>
</li>
</ul>
<p><strong>3. Frontend is Responsive</strong></p>
<ul>
<li><p>Open your app on a mobile device and desktop browser.</p>
</li>
<li><p>Ensure the design adjusts properly to different screen sizes.</p>
</li>
<li><p>Check for broken buttons, misaligned text, or hidden sections.</p>
</li>
</ul>
<p><strong>4. Continuous Integration (CI) Tests Pass</strong></p>
<ul>
<li><p>If you use GitHub Actions, make sure your tests run automatically when you push code.</p>
</li>
<li><p>Fix any failed tests before merging branches.</p>
</li>
<li><p>This helps you catch bugs early.</p>
</li>
</ul>
<p><strong>5. Documentation Files Are Complete</strong></p>
<ul>
<li><p>Ensure your <strong>README</strong>, <strong>CONTRIBUTING</strong>, and <strong>CHANGELOG</strong> files are up to date.</p>
</li>
<li><p>Add setup steps, contribution guidelines, and update notes.</p>
</li>
<li><p>This makes your repo beginner-friendly and professional.</p>
</li>
</ul>
<blockquote>
<p>Run through your README’s <strong>Quick Start</strong> section as if you’re a new user.<br>If you can set up the project in less than 10 minutes, your documentation is clear enough.</p>
</blockquote>
<h2 id="heading-common-pitfalls-amp-how-to-avoid-them-beginner-friendly">Common Pitfalls &amp; How to Avoid Them (Beginner-Friendly)</h2>
<p>Here are some common mistakes new developers make and how you can prevent them:</p>
<h3 id="heading-problem-hardcoding-api-keys">Problem: Hardcoding API Keys</h3>
<p>Never store API keys directly in your code. If you push your project to GitHub, anyone can see them.</p>
<p><strong>Solution:</strong> Store them in a <code>.env</code> file and add <code>.env</code> to <code>.gitignore</code>.</p>
<h3 id="heading-problem-no-quick-start-section">Problem: No Quick Start Section</h3>
<p>If your README doesn’t explain how to install and run the app, other developers will be lost.</p>
<p><strong>Solution:</strong> Always include a <strong>Quick Start</strong> section showing installation and setup steps.</p>
<h3 id="heading-problem-missing-example-requests-or-screenshots">Problem: Missing Example Requests or Screenshots</h3>
<p>Readers want to see what your API or app does before trying it.</p>
<p><strong>Solution:</strong> Add example API requests and responses (like the <code>/api/brand/logo</code> example). You can also include screenshots of the UI.</p>
<h3 id="heading-problem-confusing-folder-structure">Problem: Confusing Folder Structure</h3>
<p>A messy project makes it hard for contributors to navigate your code.</p>
<p><strong>Solution:</strong> Explain your folder structure under “Repository Structure.” Include short descriptions of what each folder does.</p>
<h3 id="heading-problem-forgetting-to-version-your-project">Problem: Forgetting to Version Your Project</h3>
<p>If you don’t track changes, it’s hard to know what was updated or fixed.</p>
<p><strong>Solution:</strong> Use <strong>Semantic Versioning</strong> (<code>1.0.0</code>, <code>1.1.0</code>, and so on) and keep a simple <strong>CHANGELOG.md</strong> file.</p>
<h3 id="heading-problem-no-testing-before-deployment">Problem: No Testing Before Deployment</h3>
<p>Beginners often deploy without testing—and later find bugs in production.</p>
<p><strong>Solution:</strong> Run your tests locally first. Automate them using <strong>GitHub Actions</strong> so that every code change is verified.</p>
<p>By addressing these simple issues early, you’ll build reliable, professional-looking projects that others can understand and contribute to easily.</p>
<h2 id="heading-what-you-can-learn-from-this">💡 What You Can Learn from This</h2>
<p>A good README file saves you from:</p>
<ul>
<li><p>Wasting hours debugging setup issues</p>
</li>
<li><p>Confusing collaborators or testers</p>
</li>
<li><p>Forgetting your own logic months later</p>
</li>
</ul>
<p>It also makes your project look professional to employers and recruiters.</p>
<h2 id="heading-final-words">Final Words</h2>
<p>When I finally embraced writing detailed README files, everything changed. New collaborators understood my projects faster. Deployment became smoother. And most importantly—I never had to “learn the hard way” again.</p>
<p>So if you’re just starting out, take my advice: <strong>Before you write your next line of code, write your README file.</strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a MERN Stack To-Do App ]]>
                </title>
                <description>
                    <![CDATA[ This guide will walk you through building a full-stack MERN To-Do application. It covers setting up the environment, writing code to demonstrate core CRUD (Create, Read, Update, Delete) operations, and connecting the application to MongoDB Atlas, a f... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-mern-stack-to-do-app/</link>
                <guid isPermaLink="false">67c74d6473daa61c95803cfc</guid>
                
                    <category>
                        <![CDATA[ MERN Stack ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MongoDB ]]>
                    </category>
                
                    <category>
                        <![CDATA[ todoapp ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Tue, 04 Mar 2025 18:58:44 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1741102112733/3aa43545-c095-4a47-8787-130b470f6ce1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>This guide will walk you through building a full-stack MERN To-Do application. It covers setting up the environment, writing code to demonstrate core CRUD (Create, Read, Update, Delete) operations, and connecting the application to MongoDB Atlas, a free cloud database.</p>
<p>Before diving into this article, I recommend that you have a foundational understanding of HTML, CSS, and JavaScript, as well as some knowledge of frontend and backend frameworks and libraries.</p>
<p>My primary focus will be on functionality, allowing you to customize the design as you see fit. The commands I’ll use here are tailored for Windows, so if you're using Linux, macOS, or Ubuntu, you may need to adjust them accordingly.</p>
<p>By the end of this guide, you'll have a fully functional To-Do app up and running on your system.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-introduction-to-the-mern-stack">Introduction to the MERN Stack</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-set-up-your-development-environment">How to Set Up Your Development environment</a></p>
<ul>
<li><a class="post-section-overview" href="#heading-install-nodejs-and-npm-node-package-manager">Install Node.js and npm - Node Package Manager</a></li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-set-up-a-new-mern-project">How to Set Up a New MERN Project</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-frontend-setup">Frontend Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-build-the-to-do-app-ui">Build the To-Do App UI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-displaying-your-tasks-in-the-ui">Displaying your tasks in the UI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-give-your-app-customized-styling">Give Your App Customized Styling</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-backend-setup">Backend Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-set-up-mongodb-atlas">Set Up MongoDB Atlas</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-run-the-application">Run the Application</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-introduction-to-the-mern-stack">Introduction to the MERN Stack</h2>
<p>The MERN stack is a popular JavaScript stack for building modern web applications. It consists of:</p>
<ul>
<li><p><strong>MongoDB</strong>: A NoSQL database for storing data.</p>
</li>
<li><p><strong>Express.js</strong>: A backend framework for building APIs.</p>
</li>
<li><p><strong>React (UI library) + Vite (build tool) + TypeScript (typed JavaScript)</strong>: A modern frontend stack for building scalable and maintainable user interfaces.</p>
</li>
<li><p><strong>Node.js</strong>: A runtime environment for executing JavaScript on the server.</p>
</li>
</ul>
<h2 id="heading-how-to-set-up-your-development-environment">How to Set Up Your Development environment</h2>
<h3 id="heading-install-nodejs-and-npm-node-package-manager">Install Node.js and npm (Node Package Manager)</h3>
<p>Instead of installing Node.js and npm in your project folder, I advise you to install them in your system's root directory so that you can use them in any project, not just this one.</p>
<p>First, download and install Node.js (which includes npm) from the <a target="_blank" href="https://nodejs.org/en">official website</a> if you don’t have it already.</p>
<p>After installation, open your command line (I am using Git Bash) and verify the installation by running the following commands:</p>
<pre><code class="lang-bash">node -v
npm -v
</code></pre>
<p>You should see the installed versions of Node.js and npm if correctly installed.</p>
<h2 id="heading-how-to-set-up-a-new-mern-project">How to Set Up a New MERN Project</h2>
<p>Create a project folder and open your code editor by running these commands:</p>
<pre><code class="lang-bash">mkdir mern-todo-app
<span class="hljs-built_in">cd</span> mern-todo-app
code .
</code></pre>
<p>The command <code>code .</code> automatically opens VS Code. If it doesn’t, open VS Code manually and navigate to your <code>mern-todo-app</code> folder.</p>
<h3 id="heading-frontend-setup">Frontend Setup</h3>
<h4 id="heading-set-up-vite-with-react-and-typescript">Set Up Vite with React and TypeScript</h4>
<p>Make sure you are in your project root directory (<code>mern-todo-app</code>), then run the following command:</p>
<pre><code class="lang-bash">npm create vite@latest frontend --template react-ts
</code></pre>
<p>This command will create a TypeScript-based React frontend inside the <code>frontend</code> folder within your <code>mern-todo-app</code> directory.</p>
<h4 id="heading-install-axios-for-making-api-requests">Install Axios for Making API Requests</h4>
<p>Axios is a popular JavaScript library used to make HTTP requests from the frontend to a backend API. It simplifies sending GET, POST, PUT, and DELETE requests and handling responses.</p>
<p>To install Axios, run the following command:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> frontend
npm install axios
</code></pre>
<h3 id="heading-build-the-to-do-app-ui">Build the To-Do App UI</h3>
<p>Inside the <code>src</code> folder, create an <code>App.tsx</code> file if it doesn’t already exist, and add the below code. It’s a lot, but don’t worry – I’ll break it down bit by bit afterwards:</p>
<p><code>frontend/src/App.tsx</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// BLOCK 1: Importing Dependencies</span>
<span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">"axios"</span>;
<span class="hljs-keyword">import</span> TodoList <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/TodoList.tsx"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./App.css"</span>;

<span class="hljs-comment">// BLOCK 2: Defining Task Interface</span>
interface Task {
  <span class="hljs-attr">_id</span>: string;
  title: string;
  completed: boolean;
}

<span class="hljs-comment">// BLOCK 3: Setting Up State Variables</span>
<span class="hljs-keyword">const</span> App: React.FC = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">// State for tasks, new task text, and editing controls</span>
  <span class="hljs-keyword">const</span> [tasks, setTasks] = useState&lt;Task[]&gt;([]);
  <span class="hljs-keyword">const</span> [task, setTask] = useState&lt;string&gt;(<span class="hljs-string">""</span>);
  <span class="hljs-keyword">const</span> [editingTaskId, setEditingTaskId] = useState&lt;string | <span class="hljs-literal">null</span>&gt;(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [editingTitle, setEditingTitle] = useState&lt;string&gt;(<span class="hljs-string">""</span>);

  <span class="hljs-comment">// BLOCK 4: Fetch tasks from the backend on component mount</span>
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> fetchTasks = <span class="hljs-keyword">async</span> () =&gt; {
      <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get&lt;Task[]&gt;(<span class="hljs-string">`http://localhost:5000/api/tasks`</span>);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Fetched tasks:"</span>, response.data); <span class="hljs-comment">// Debugging log</span>
        setTasks(response.data);
      } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Error fetching tasks:"</span>, error);
      }
    };
    fetchTasks();
  }, []);

  <span class="hljs-comment">// BLOCK 5: Adding a Task</span>
  <span class="hljs-keyword">const</span> addTask = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">if</span> (!task) <span class="hljs-keyword">return</span>;

    <span class="hljs-keyword">try</span> {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Adding task:"</span>, task); <span class="hljs-comment">// Debugging log</span>
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.post&lt;Task&gt;(
        <span class="hljs-string">`http://localhost:5000/api/tasks`</span>,
        { <span class="hljs-attr">title</span>: task },
        { <span class="hljs-attr">headers</span>: { <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span> } }
      );
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Task added response:"</span>, response.data);
      setTasks([...tasks, response.data]);
      setTask(<span class="hljs-string">""</span>);
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Error adding task:"</span>, error);
    }
  };

  <span class="hljs-comment">// BLOCK 6: Delete a task</span>
  <span class="hljs-keyword">const</span> deleteTask = <span class="hljs-keyword">async</span> (id: string) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">await</span> axios.delete(<span class="hljs-string">`http://localhost:5000/api/tasks/<span class="hljs-subst">${id}</span>`</span>);
      setTasks(tasks.filter(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span> t._id !== id));
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Error deleting task:"</span>, error);
    }
  };

  <span class="hljs-comment">// BLOCK 7: Updating a Task</span>
  <span class="hljs-keyword">const</span> updateTask = <span class="hljs-keyword">async</span> (id: string, <span class="hljs-attr">updatedTask</span>: Partial&lt;Task&gt;) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.put(
        <span class="hljs-string">`http://localhost:5000/api/tasks/<span class="hljs-subst">${id}</span>`</span>,
        updatedTask,
        { <span class="hljs-attr">headers</span>: { <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span> } }
      );

      setTasks(
        tasks.map(<span class="hljs-function">(<span class="hljs-params">task</span>) =&gt;</span>
          task._id === id ? { ...task, ...response.data } : task
        )
      );
      setEditingTaskId(<span class="hljs-literal">null</span>);
      setEditingTitle(<span class="hljs-string">""</span>);
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Error updating task:"</span>, error);
    }
  };

  <span class="hljs-comment">// BLOCK 8: Handling Edits</span>
  <span class="hljs-keyword">const</span> startEditing = <span class="hljs-function">(<span class="hljs-params">id: string</span>) =&gt;</span> {
    setEditingTaskId(id);
  };

  <span class="hljs-comment">// Handle title change during editing</span>
  <span class="hljs-keyword">const</span> handleEditChange = <span class="hljs-function">(<span class="hljs-params">e: React.ChangeEvent&lt;HTMLInputElement&gt;</span>) =&gt;</span> {
    setEditingTitle(e.target.value);
  };

  <span class="hljs-comment">// BLOCK 9: Render the app</span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Todo App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
          <span class="hljs-attr">value</span>=<span class="hljs-string">{task}</span>
          <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> setTask(e.target.value)}
        /&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{addTask}</span>&gt;</span>Add Task<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TodoList</span>
        <span class="hljs-attr">tasks</span>=<span class="hljs-string">{tasks}</span>
        <span class="hljs-attr">deleteTask</span>=<span class="hljs-string">{deleteTask}</span>
        <span class="hljs-attr">updateTask</span>=<span class="hljs-string">{updateTask}</span>
        <span class="hljs-attr">editingTitle</span>=<span class="hljs-string">{editingTitle}</span>
        <span class="hljs-attr">setEditingTitle</span>=<span class="hljs-string">{setEditingTitle}</span>
        <span class="hljs-attr">editingTaskId</span>=<span class="hljs-string">{editingTaskId}</span>
        <span class="hljs-attr">setEditingTaskId</span>=<span class="hljs-string">{setEditingTaskId}</span>
        <span class="hljs-attr">startEditing</span>=<span class="hljs-string">{startEditing}</span>
        <span class="hljs-attr">handleEditChange</span>=<span class="hljs-string">{handleEditChange}</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-comment">// BLOCK 10: Exporting the Component</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>Here’s a block-by-block breakdown of the code above:</p>
<p><strong>BLOCK 1:</strong> Importing dependencies</p>
<ul>
<li><p><code>React, { useState, useEffect }</code>: Manages component state and side effects.</p>
</li>
<li><p><code>axios</code>: Handles API requests.</p>
</li>
<li><p><code>TodoList.tsx</code>: A child component to display and manage tasks.</p>
</li>
<li><p><code>App.css</code>: Styles the app.</p>
</li>
</ul>
<p><strong>BLOCK 2:</strong> Defining the task interface</p>
<ul>
<li>Defines the structure of a task (<code>_id</code>, <code>title</code>, <code>completed</code>).</li>
</ul>
<p><strong>BLOCK 3:</strong> Setting up state variables</p>
<ul>
<li><p><code>tasks</code>: Stores the list of tasks.</p>
</li>
<li><p><code>task</code>: Holds input for new tasks.</p>
</li>
<li><p><code>editingTaskId</code>: Tracks the task being edited.</p>
</li>
<li><p><code>editingTitle</code>: Stores the updated title while editing.</p>
</li>
</ul>
<p><strong>BLOCK 4:</strong> Fetching tasks from the backend (<code>useEffect</code>)</p>
<ul>
<li><p>Runs once when the app loads.</p>
</li>
<li><p>Calls the API (<code>GET /api/tasks</code>) to get tasks and updates <code>tasks</code>.</p>
</li>
<li><p>Error handling**:** Logs an error message if the fetching request fails</p>
</li>
</ul>
<p><strong>BLOCK 5:</strong> Adding a task</p>
<ul>
<li><p>Sends a <code>POST</code> request to add a new task.</p>
</li>
<li><p>Updates <code>tasks</code> with the new task.</p>
</li>
<li><p>Error handling**:** Logs an error message if the adding task request fails</p>
</li>
</ul>
<p><strong>BLOCK 6:</strong> Deleting a task</p>
<ul>
<li><p>Sends a <code>DELETE</code> request to remove a task.</p>
</li>
<li><p>Updates <code>tasks</code> by filtering out the deleted task.</p>
</li>
<li><p>Error handling**:** Logs an error message if the deleting task request fails</p>
</li>
</ul>
<p><strong>BLOCK 7:</strong> Updating a task</p>
<ul>
<li><p>Sends a <code>PUT</code> request to update a task’s title.</p>
</li>
<li><p>Updates <code>tasks</code> with the new title.</p>
</li>
<li><p>Error handling**:** Logs an error message if the update request fails</p>
</li>
</ul>
<p><strong>BLOCK 8:</strong> Handling edits</p>
<ul>
<li><p><code>startEditing(id)</code>: Sets a task into edit mode.</p>
</li>
<li><p><code>handleEditChange(e)</code>: Updates the editing input.</p>
</li>
</ul>
<p><strong>BLOCK 9:</strong> Rendering the UI</p>
<ul>
<li><p>Displays an input field and button to add tasks.</p>
</li>
<li><p>Passes task data and functions (<code>deleteTask</code>, <code>updateTask</code>, etc.) to <code>TodoList.tsx</code>.</p>
</li>
</ul>
<p><strong>BLOCK 10:</strong> Exporting the component</p>
<ul>
<li><code>export default App;</code>: Makes <code>App</code> usable in other files.</li>
</ul>
<h3 id="heading-displaying-your-tasks-in-the-ui">Displaying your tasks in the UI</h3>
<p>Inside the <code>src</code> folder, create a new folder named <code>components</code>. Then add a <code>TodoList.tsx</code> file inside it with the below code.</p>
<p><code>src/components/TodoList.tsx</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// BLOCK 1: Importing Dependencies</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-comment">// BLOCK 2: Defining Interfaces</span>
interface Task {
  <span class="hljs-attr">_id</span>: string; <span class="hljs-comment">// Unique ID for the task</span>
  title: string; <span class="hljs-comment">// Task name</span>
  completed: boolean; <span class="hljs-comment">// True if done, False if not</span>
}

interface TodoListProps {
  <span class="hljs-attr">tasks</span>: Task[];
  deleteTask: <span class="hljs-function">(<span class="hljs-params">id: string</span>) =&gt;</span> <span class="hljs-keyword">void</span>;
  updateTask: <span class="hljs-function">(<span class="hljs-params">id: string, updatedTask: Partial&lt;Task&gt;</span>) =&gt;</span> <span class="hljs-keyword">void</span>;
  editingTitle: string;
  setEditingTitle: <span class="hljs-function">(<span class="hljs-params">title: string</span>) =&gt;</span> <span class="hljs-keyword">void</span>;
  editingTaskId: string | <span class="hljs-literal">null</span>;
  setEditingTaskId: <span class="hljs-function">(<span class="hljs-params">id: string | <span class="hljs-literal">null</span></span>) =&gt;</span> <span class="hljs-keyword">void</span>;
  startEditing: <span class="hljs-function">(<span class="hljs-params">id: string</span>) =&gt;</span> <span class="hljs-keyword">void</span>;
  handleEditChange: <span class="hljs-function">(<span class="hljs-params">e: React.ChangeEvent&lt;HTMLInputElement&gt;</span>) =&gt;</span> <span class="hljs-keyword">void</span>;
}

<span class="hljs-comment">// BLOCK 3: Declares the TodoList Component</span>
<span class="hljs-keyword">const</span> TodoList: React.FC&lt;TodoListProps&gt; = <span class="hljs-function">(<span class="hljs-params">{
  tasks,
  deleteTask,
  updateTask,
  editingTitle,
  setEditingTitle,
  editingTaskId,
  setEditingTaskId,
  startEditing,
  handleEditChange,
}</span>) =&gt;</span> {

  <span class="hljs-comment">// BLOCK 4: Rendering the Task List and handling task actions</span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {tasks.map((task) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{task._id}</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
            <span class="hljs-attr">type</span>=<span class="hljs-string">"checkbox"</span>
            <span class="hljs-attr">checked</span>=<span class="hljs-string">{task.completed}</span>
            <span class="hljs-attr">onChange</span>=<span class="hljs-string">{()</span> =&gt;</span> updateTask(task._id, { completed: !task.completed })}
          /&gt;
          {editingTaskId === task._id ? (
            <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{editingTitle}</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleEditChange}</span> /&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> {
                  updateTask(task._id, { title: editingTitle });
                  setEditingTaskId(null);
                }}
              &gt;
                Save
              <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span>
          ) : (
            <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">textDecoration:</span> <span class="hljs-attr">task.completed</span> ? "<span class="hljs-attr">line-through</span>" <span class="hljs-attr">:</span> "<span class="hljs-attr">none</span>" }}&gt;</span>
                {task.title}
              <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>

              <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> deleteTask(task._id)}&gt;Delete<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> {
                    startEditing(task._id);
                    setEditingTitle(task.title);
                  }}
                &gt;
                  Edit
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span></span>
          )}
        &lt;/li&gt;
      ))}
    &lt;/ul&gt;
  );
};

<span class="hljs-comment">// BLOCK 5: Exporting the Component</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> TodoList;
</code></pre>
<p>Here’s a block-by-block breakdown of the code above:</p>
<p><strong>BLOCK 1:</strong> Importing dependencies</p>
<ul>
<li>React: Enables functional component creation.</li>
</ul>
<p><strong>BLOCK 2:</strong> Defining interfaces</p>
<ul>
<li><p>Task interface: Defines <code>_id</code>, <code>title</code>, and <code>completed</code> properties.</p>
</li>
<li><p>TodoListProps interface: Defines props passed to the <code>TodoList</code> component</p>
</li>
</ul>
<p><strong>BLOCK 3</strong>: Declares the <code>TodoList</code> component</p>
<ul>
<li><p>Defines a functional React component (<code>TodoList</code>) using TypeScript (<code>React.FC&lt;TodoListProps&gt;</code>).</p>
</li>
<li><p>Extracts the listed props from <code>TodoListProps</code> and prepares the component for rendering.</p>
</li>
</ul>
<p><strong>BLOCK 4</strong>: Rendering the Task List and handling task actions</p>
<ul>
<li><p>Maps through <code>tasks</code> to display each task inside a <code>&lt;ul&gt;</code>.</p>
</li>
<li><p>Checkbox toggles <code>completed</code> status using <code>updateTask()</code>.</p>
</li>
<li><p>Conditional rendering:</p>
<ul>
<li><p>If a task is being edited, an input field appears for editing.</p>
</li>
<li><p>Otherwise, the task title is displayed with a strikethrough if completed</p>
</li>
</ul>
</li>
<li><p>Save button: Updates the task title using <code>updateTask()</code>, then exits edit mode.</p>
</li>
<li><p>Delete button: Calls <code>deleteTask()</code> to remove a task.</p>
</li>
<li><p>Edit button: Enables edit mode, setting <code>editingTaskId</code> and <code>editingTitle</code>.</p>
</li>
</ul>
<p><strong>BLOCK 5</strong>: Exporting the component</p>
<ul>
<li>Makes <code>TodoList</code> available for use in other components.</li>
</ul>
<h3 id="heading-give-your-app-customized-styling">Give Your App Customized Styling</h3>
<p>Inside your <code>src</code> folder, create <code>App.css</code> if it doesn’t exist and replace the content with your desired styling. Let’s give the frontend a finishing touch.</p>
<p><code>src/App.css</code>:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Center the app in the middle of the screen */</span>
<span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-family</span>: Arial, sans-serif;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f4f4f4</span>; <span class="hljs-comment">/* Light gray background */</span>
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">overflow-x</span>: hidden; <span class="hljs-comment">/* Prevent horizontal scrolling */</span>
}

<span class="hljs-comment">/* Style the main app container */</span>
<span class="hljs-selector-class">.App</span> {
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">background</span>: white;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>; <span class="hljs-comment">/* Rounded corners */</span>
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">10px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>); <span class="hljs-comment">/* Light shadow effect */</span>
  <span class="hljs-attribute">width</span>: <span class="hljs-number">90%</span>; <span class="hljs-comment">/* Make it flexible */</span>
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">350px</span>; <span class="hljs-comment">/* Prevent exceeding max size */</span>
  <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-comment">/* Add spacing below the title */</span>
<span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">24px</span>;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#333</span>;
}

<span class="hljs-comment">/* Style input fields */</span>
<span class="hljs-selector-tag">input</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-comment">/* Full width */</span>
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ccc</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-comment">/* Style buttons */</span>
<span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-comment">/* Make buttons full width */</span>
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#007bff</span>; <span class="hljs-comment">/* Blue background */</span>
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">transition</span>: background-color <span class="hljs-number">0.3s</span> ease-in-out;
}

<span class="hljs-comment">/* Change button color when hovered */</span>
<span class="hljs-selector-tag">button</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0056b3</span>;
}

<span class="hljs-comment">/* Remove default list styles */</span>
<span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">list-style</span>: none;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-comment">/* Style list items */</span>
<span class="hljs-selector-tag">li</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">justify-content</span>: space-between;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">2px</span> <span class="hljs-number">5px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>); <span class="hljs-comment">/* Add a subtle shadow */</span>
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-comment">/* Allow task text to take available space */</span>
<span class="hljs-selector-tag">span</span> {
  <span class="hljs-attribute">flex</span>: <span class="hljs-number">1</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#333</span>;
}

<span class="hljs-comment">/* Style completed tasks */</span>
<span class="hljs-selector-tag">span</span><span class="hljs-selector-class">.completed</span> {
  <span class="hljs-attribute">text-decoration</span>: line-through;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#888</span>;
}

<span class="hljs-comment">/* Adjust the width of input fields inside the list */</span>
<span class="hljs-selector-tag">input</span><span class="hljs-selector-attr">[type=<span class="hljs-string">"text"</span>]</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">70%</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ccc</span>;
}

<span class="hljs-comment">/* Style the checkbox */</span>
<span class="hljs-selector-tag">input</span><span class="hljs-selector-attr">[type=<span class="hljs-string">"checkbox"</span>]</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">18px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">18px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">accent-color</span>: <span class="hljs-number">#007bff</span>; <span class="hljs-comment">/* Blue checkbox to match buttons */</span>
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">10px</span>;
}

<span class="hljs-comment">/* Styling for editing mode */</span>
<span class="hljs-selector-class">.editing-container</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-comment">/* Responsive styling for smaller screens */</span>
<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">400px</span>) {
  <span class="hljs-selector-class">.App</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">95%</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span>;
    <span class="hljs-attribute">max-width</span>: none; <span class="hljs-comment">/* Remove fixed width restriction */</span>
  }

  <span class="hljs-selector-tag">li</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">align-items</span>: flex-start;
  }

  <span class="hljs-selector-tag">input</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  }

  <span class="hljs-selector-tag">button</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  }
}
</code></pre>
<p>Here’s what this CSS code does:</p>
<p>First, it centers the app (<code>html, body</code>):</p>
<ul>
<li><p>Uses <code>flexbox</code> to center the app vertically and horizontally.</p>
</li>
<li><p>Sets <code>height: 100vh</code> for full-screen height.</p>
</li>
<li><p>Prevents horizontal scrolling with <code>overflow-x: hidden</code>.</p>
</li>
</ul>
<p>Then it styles the main app container (<code>.App</code>):</p>
<ul>
<li><p>Adds a white background with rounded corners and a shadow.</p>
</li>
<li><p>Ensures responsiveness with <code>width: 90%</code> and <code>max-width: 350px</code>.</p>
</li>
</ul>
<p>Next, we handle typography and layout:</p>
<ul>
<li><p>Sets <code>Arial, sans-serif</code> as the font.</p>
</li>
<li><p>Adds spacing below the title (<code>h1</code>).</p>
</li>
<li><p>Ensures task text takes available space with <code>span { flex: 1; }</code>.</p>
</li>
</ul>
<p>Then we deal with input and button styling:</p>
<ul>
<li><p>Inputs are full-width, styled with padding, borders, and rounded corners.</p>
</li>
<li><p>Buttons are blue, full-width, with hover effects (<code>background-color: #0056b3</code>).</p>
</li>
</ul>
<p>And then task list styling <strong>(</strong><code>ul, li, span.completed</code>):</p>
<ul>
<li><p>Removes default list styles.</p>
</li>
<li><p>Each task (<code>li</code>) has a white background, padding, rounded corners, and a shadow.</p>
</li>
<li><p>Completed tasks are styled with a <code>line-through</code> and faded text color.</p>
</li>
</ul>
<p>Next, we handle checkbox and editing mode styling:</p>
<ul>
<li><p>Styled blue checkboxes (<code>accent-color: #007bff</code>).</p>
</li>
<li><p>Adds an <code>editing-container</code> with <code>display: flex;</code> for edit mode.</p>
</li>
</ul>
<p>And finally, we make the design responsive (<code>@media (max-width: 400px)</code>):</p>
<ul>
<li><p>Adjusts <code>.App</code> width and padding for small screens.</p>
</li>
<li><p>Stacks list items (<code>li</code>) vertically instead of side-by-side.</p>
</li>
</ul>
<h3 id="heading-backend-setup">Backend Setup</h3>
<p>In your VS Code terminal, make sure you are in your project root directory (inside <code>mern-todo-app</code>) and then create a folder called <code>backend</code>. Navigate to the <code>backend</code> folder and initialize <code>Node.js</code>:</p>
<pre><code class="lang-bash">mkdir backend
<span class="hljs-built_in">cd</span> backend
npm init -y
</code></pre>
<h4 id="heading-install-dependencies">Install Dependencies</h4>
<p>Still inside your <code>backend</code> folder, run this command:</p>
<pre><code class="lang-bash">npm install express mongoose dotenv cors
</code></pre>
<p>In this command,</p>
<ul>
<li><p><code>express</code> is a fast and minimal web framework for Node.js used to create server-side applications and APIs.</p>
</li>
<li><p><code>mongoose</code> is an Object Data Modeling (ODM) library for MongoDB, simplifying database interactions.</p>
</li>
<li><p><code>dotenv</code> loads environment variables from a <code>.env</code> file, keeping sensitive data secure.</p>
</li>
<li><p><code>cors</code> enables Cross-Origin Resource Sharing, allowing frontend applications to communicate with the backend across different domains.</p>
</li>
</ul>
<h4 id="heading-create-a-serverjs-file">Create a server.js File</h4>
<p>Inside your <code>backend</code> folder, create a file named <code>server.js</code> and enter the following code:</p>
<p><code>backend/server.js</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// BLOCK 1: Importing Dependencies</span>
<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">"mongoose"</span>);
<span class="hljs-keyword">const</span> cors = <span class="hljs-built_in">require</span>(<span class="hljs-string">"cors"</span>);
<span class="hljs-keyword">const</span> dotenv = <span class="hljs-built_in">require</span>(<span class="hljs-string">"dotenv"</span>);

<span class="hljs-comment">// BLOCK 2: Configuring the Express App</span>
dotenv.config();

<span class="hljs-keyword">const</span> app = express();

<span class="hljs-comment">// BLOCK 3: Setting Up Middleware</span>
app.use(cors());
app.use(express.json());

<span class="hljs-comment">// BLOCK 4: Connecting to MongoDB</span>
<span class="hljs-keyword">const</span> connectDB = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">await</span> mongoose.connect(process.env.MONGO_URI);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"MongoDB Connected"</span>);
  } <span class="hljs-keyword">catch</span> (err) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"MongoDB Connection Failed:"</span>, err);
    process.exit(<span class="hljs-number">1</span>); <span class="hljs-comment">// Exit process with failure</span>
  }
};

<span class="hljs-comment">// Call the database connection function</span>
connectDB();

<span class="hljs-comment">// BLOCK 5: Defining Routes</span>
<span class="hljs-keyword">const</span> tasksRoutes = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./routes/tasks"</span>);
app.use(<span class="hljs-string">"/api/tasks"</span>, tasksRoutes);

<span class="hljs-comment">// BLOCK 6: Starting the Server</span>
<span class="hljs-keyword">const</span> PORT = process.env.PORT || <span class="hljs-number">5000</span>;
app.listen(PORT, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Server running on port <span class="hljs-subst">${PORT}</span>`</span>);
});
</code></pre>
<p>Here’s a block-by-block breakdown of the code above:</p>
<p><strong>BLOCK 1</strong>: Importing dependencies</p>
<ul>
<li><p>express**:** Creates the server.</p>
</li>
<li><p>mongoose**:** Connects to MongoDB.</p>
</li>
<li><p>cors**:** Enables cross-origin requests.</p>
</li>
<li><p>dotenv**:** Loads environment variables.</p>
</li>
</ul>
<p><strong>BLOCK 2</strong>: Configuring the Express app</p>
<ul>
<li><p>Loads environment variables using <code>dotenv.config()</code>.</p>
</li>
<li><p>Initializes <code>express()</code> to create an app instance.</p>
</li>
</ul>
<p><strong>BLOCK 3</strong>: Setting up middleware</p>
<ul>
<li><p>cors()<strong>:</strong> Allows API access from different origins.</p>
</li>
<li><p>express.json()<strong>:</strong> Parses incoming JSON requests.</p>
</li>
</ul>
<p><strong>BLOCK 4</strong>: Connecting to MongoDB</p>
<ul>
<li><p>Defines <code>connectDB()</code> to connect to MongoDB using <code>MONGO_URI</code>.</p>
</li>
<li><p>Logs success or failure and exits on error.</p>
</li>
</ul>
<p><strong>BLOCK 5</strong>: Defining routes</p>
<ul>
<li><p>Imports <code>tasksRoutes</code> from <code>./routes/tasks</code>.</p>
</li>
<li><p>Uses <code>/api/tasks</code> as the base route for task operations.</p>
</li>
</ul>
<p><strong>BLOCK 6</strong>: Starting the server</p>
<ul>
<li><p>Sets <code>PORT</code> from <code>.env</code> or defaults to <code>5000</code>.</p>
</li>
<li><p>Starts the server and logs the running port.</p>
</li>
</ul>
<h4 id="heading-define-task-model">Define Task Model</h4>
<p>In your <code>backend</code> folder, create a <code>model</code> folder. Inside <code>model</code>, create a file named <code>Task.js</code> and add the following code:</p>
<p><code>backend/model/Task.js</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// BLOCK 1: Importing Mongoose</span>
<span class="hljs-keyword">const</span> mongoose = <span class="hljs-built_in">require</span>(<span class="hljs-string">"mongoose"</span>);

<span class="hljs-comment">// BLOCK 2: Defining Task Schema</span>
<span class="hljs-keyword">const</span> TaskSchema = <span class="hljs-keyword">new</span> mongoose.Schema({
    <span class="hljs-attr">title</span>: { <span class="hljs-attr">type</span>: <span class="hljs-built_in">String</span>, <span class="hljs-attr">required</span>: <span class="hljs-literal">true</span> },
    <span class="hljs-attr">completed</span>: { <span class="hljs-attr">type</span>: <span class="hljs-built_in">Boolean</span>, <span class="hljs-attr">default</span>: <span class="hljs-literal">false</span> },
});

<span class="hljs-comment">// BLOCK 3: Creating and Exporting the Model</span>
<span class="hljs-built_in">module</span>.exports = mongoose.model(<span class="hljs-string">"Task"</span>, TaskSchema);
</code></pre>
<p>Here’s a block-by-block breakdown of the code above:</p>
<p><strong>BLOCK 1</strong>: Importing Mongoose</p>
<ul>
<li><code>mongoose</code>: Used to define the schema and interact with MongoDB.</li>
</ul>
<p><strong>BLOCK 2</strong>: Defining the task schema</p>
<ul>
<li><p><code>title</code>: A required string field for the task title.</p>
</li>
<li><p><code>completed</code>: A boolean field indicating task status (default: <code>false</code>).</p>
</li>
</ul>
<p><strong>BLOCK 3</strong>: Creating and exporting the model</p>
<ul>
<li><p>Creates a Mongoose model named <code>"Task"</code> based on <code>TaskSchema</code>.</p>
</li>
<li><p>Exports the model for use in other parts of the application</p>
</li>
</ul>
<h4 id="heading-define-routes">Define Routes</h4>
<p>In your <code>backend</code> folder, create a <code>routes</code> folder. Inside <code>routes</code>, create a file named <code>tasks.js</code> and add the following code:</p>
<p><code>backend/routes/tasks.js</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// BOCK 1: Import dependencies</span>
<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> Task = <span class="hljs-built_in">require</span>(<span class="hljs-string">"../models/Task"</span>);

<span class="hljs-keyword">const</span> router = express.Router();

<span class="hljs-comment">// BLOCK 2: GET all tasks</span>
router.get(<span class="hljs-string">"/"</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> tasks = <span class="hljs-keyword">await</span> Task.find();
    res.json(tasks);
  } <span class="hljs-keyword">catch</span> (err) {
    res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: err.message });
  }
});

<span class="hljs-comment">// BLOCK 3: POST a new task</span>
router.post(<span class="hljs-string">"/"</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">const</span> { title } = req.body;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Received title:"</span>, title); <span class="hljs-comment">// Debugging log</span>

  <span class="hljs-keyword">if</span> (!title) {
    <span class="hljs-keyword">return</span> res.status(<span class="hljs-number">400</span>).json({ <span class="hljs-attr">error</span>: <span class="hljs-string">"Task title is required"</span> });
  }

  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> newTask = <span class="hljs-keyword">new</span> Task({ title });
    <span class="hljs-keyword">await</span> newTask.save();
    res.status(<span class="hljs-number">201</span>).json(newTask);
  } <span class="hljs-keyword">catch</span> (err) {
    <span class="hljs-built_in">console</span>.error(err.message);
    res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: err.message });
  }
});

<span class="hljs-comment">// BLOCK 4: DELETE a task</span>
router.delete(<span class="hljs-string">"/:id"</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> { id } = req.params;
    <span class="hljs-keyword">await</span> Task.findByIdAndDelete(id);
    res.json({ <span class="hljs-attr">message</span>: <span class="hljs-string">"Task deleted"</span> });
  } <span class="hljs-keyword">catch</span> (err) {
    res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: err.message });
  }
});

<span class="hljs-comment">// BLOCK 5: UPDATE a task</span>
router.put(<span class="hljs-string">"/:id"</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> { id } = req.params;

    <span class="hljs-keyword">const</span> updatedTask = <span class="hljs-keyword">await</span> Task.findByIdAndUpdate(
      id,
      req.body,
      { <span class="hljs-attr">new</span>: <span class="hljs-literal">true</span> } <span class="hljs-comment">// Return the updated task</span>
    );
    res.json(updatedTask);
  } <span class="hljs-keyword">catch</span> (error) {
    res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: <span class="hljs-string">"Error updating task"</span> });
  }
});

<span class="hljs-comment">// BLOCK 6: Export the router</span>
<span class="hljs-built_in">module</span>.exports = router;
</code></pre>
<p>Here’s a block-by-block breakdown of the code above:</p>
<p><strong>BLOCK 1</strong>: Import dependencies</p>
<ul>
<li><p>express: Handles routing.</p>
</li>
<li><p>Task: Imports the Task model.</p>
</li>
<li><p>express.Router(): Creates a router for task-related routes.</p>
</li>
</ul>
<p><strong>BLOCK 2</strong>: GET all tasks</p>
<ul>
<li><p>Fetches all tasks from the database.</p>
</li>
<li><p>Sends the tasks as a JSON response.</p>
</li>
<li><p>Handles errors with a 500 status.</p>
</li>
</ul>
<p><strong>BLOCK 3</strong>: POST a new task</p>
<ul>
<li><p>Extracts <code>title</code> from the request body.</p>
</li>
<li><p>Logs the received title for debugging.</p>
</li>
<li><p>Validates the title (returns 400 if missing).</p>
</li>
<li><p>Saves the task to the database and returns it.</p>
</li>
</ul>
<p><strong>BLOCK 4</strong>: DELETE a task</p>
<ul>
<li><p>Extracts <code>id</code> from the request params.</p>
</li>
<li><p>Deletes the task from the database.</p>
</li>
<li><p>Returns a success message.</p>
</li>
</ul>
<p><strong>BLOCK 5</strong>: UPDATE a task</p>
<ul>
<li><p>Extracts <code>id</code> from the request params.</p>
</li>
<li><p>Updates the task using request body data.</p>
</li>
<li><p>Returns the updated task.</p>
</li>
</ul>
<p><strong>BLOCK 6</strong>: Export the router</p>
<ul>
<li>Exports <code>router</code> for use in other parts of the app.</li>
</ul>
<p>This Express.js router handles CRUD operations for a <code>Task</code> model using MongoDB. It defines routes to get all tasks, add a new task, delete a task by ID, and update a task's title by ID. Error handling ensures proper responses for missing data or server issues.</p>
<h4 id="heading-create-a-env-file">Create a .env file</h4>
<p>In your backend folder, create a <code>.env</code> file and add the following:</p>
<p><code>backend/.env</code>:</p>
<pre><code class="lang-javascript">MONGO_URI=your_mongodb_atlas_uri
</code></pre>
<h3 id="heading-set-up-mongodb-atlas">Set Up MongoDB Atlas</h3>
<p>MongoDB Atlas is a cloud-based MongoDB service. We'll use the free tier for this project.</p>
<p>To get started, go to <a target="_blank" href="https://www.mongodb.com/products/platform/atlas-database">MongoDB Atlas</a> and create an account or log in.</p>
<p>Follow the steps to create a free cluster. Once the cluster is created, click Connect and follow the instructions to:</p>
<ul>
<li><p>Whitelist your IP address to allow MongoDB access using your environment variable.</p>
</li>
<li><p>Create a database user.</p>
</li>
<li><p>Get the connection string.</p>
</li>
</ul>
<p>Replace the <code>your_mongodb_atlas_uri</code> in <code>.env</code> file with your MongoDB Atlas connection string.</p>
<p>If you are still not comfortable with how to set up MongoDB atlas, read this: <a target="_blank" href="https://www.freecodecamp.org/news/get-started-with-mongodb-atlas/">MongoDB Atlas Tutorial – How to Get Started</a>.</p>
<h3 id="heading-run-the-application">Run the Application</h3>
<p>To run the application successfully using <code>npm run dev</code>, you need to install a dependency that will start both the frontend and backend simultaneously. You can do this using <a target="_blank" href="https://www.npmjs.com/package/concurrently">concurrently</a><strong>.</strong></p>
<p>Install concurrently:</p>
<p>Open your terminal, navigate to your project root directory (<code>mern-todo-app</code>), and run:</p>
<pre><code class="lang-bash">npm install concurrently
</code></pre>
<h4 id="heading-configure-packagejson">Configure <code>package.json</code>:</h4>
<p>After installing concurrently, ensure that you have a <code>package.json</code> file in your project root directory. If it doesn't exist, create one and add the following code:</p>
<p><code>mern-todo-app/package.json</code>:</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"mern-todo-app"</span>,
    <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
    <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">"scripts"</span>: {
        <span class="hljs-attr">"start"</span>: <span class="hljs-string">"cd backend &amp;&amp; npm start"</span>,
        <span class="hljs-attr">"client"</span>: <span class="hljs-string">"cd frontend &amp;&amp; npm run dev"</span>,
        <span class="hljs-attr">"dev"</span>: <span class="hljs-string">"concurrently \"npm run start\" \"npm run client\""</span>
    },
    <span class="hljs-attr">"dependencies"</span>: {
        <span class="hljs-attr">"concurrently"</span>: <span class="hljs-string">"^8.2.2"</span>
    }
}
</code></pre>
<p>This <code>package.json</code> file configures the application by defining:</p>
<ul>
<li><p>Project metadata (<code>name, version, private flag</code>).</p>
</li>
<li><p>Scripts (<code>start</code>, <code>client</code>, and <code>dev</code>) to start the backend, run the frontend, and execute both simultaneously.</p>
</li>
<li><p>Dependencies, including <code>concurrently</code>, which enables running multiple scripts in parallel.</p>
</li>
<li><p>The project is set to private to prevent accidental publishing.</p>
</li>
</ul>
<h4 id="heading-start-the-application">Start the Application</h4>
<p>Ensure everything is set up and saved, then run the following command from the project root:</p>
<pre><code class="lang-bash">npm run dev
</code></pre>
<p>If the application starts successfully, you should see messages like:</p>
<pre><code class="lang-nginx"><span class="hljs-attribute">Server</span> running <span class="hljs-literal">on</span> port <span class="hljs-number">5000</span>
MongoDB Connected
</code></pre>
<h4 id="heading-view-the-application">View the Application</h4>
<p>Open your browser and navigate to:</p>
<ul>
<li><p>Frontend (To-Do app interface)<strong>:</strong> <strong>http://localhost:5173</strong></p>
</li>
<li><p>Backend (Stored tasks in the database)<strong>:</strong> <strong>http://localhost:5000/api/tasks</strong></p>
</li>
</ul>
<p>Test the functionality by adding, editing, saving, deleting tasks, and checking off completed tasks to ensure everything works properly.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Congratulations! You have successfully built a MERN To-Do app. You can further enhance it by adding features such as time and date tracking, and deploying it to a cloud platform.</p>
<p>Feel free to copy the code or clone the <a target="_blank" href="https://github.com/nuelcas/mern-todo-app.git">GitHub</a> repository to add more functionalities and customize the styling to your preference. If you found this guide helpful, please consider sharing it and <a target="_blank" href="https://www.linkedin.com/in/casmir-onyekani/">connecting</a> with me!</p>
<p>For more learning resources:</p>
<ul>
<li><p><a target="_blank" href="https://react.dev/">React.js Docs</a></p>
</li>
<li><p><a target="_blank" href="https://www.typescriptlang.org/">TypeScript Docs</a></p>
</li>
<li><p><a target="_blank" href="https://vite.dev/">Vite Docs</a></p>
</li>
<li><p><a target="_blank" href="https://www.mongodb.com/docs/">MongoDB Docs</a></p>
</li>
<li><p><a target="_blank" href="https://nodejs.org/docs/latest/api/">Node.js Docs</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Object-Oriented Programming in Python – Key OOP Concepts and Interview Questions for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ OOP is a crucial concept that every developer should grasp, especially when getting ready for job interviews. It helps you organize code into modular and reusable sections, which simplifies the development, maintenance, and scaling of software applic... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/object-oriented-programming-in-python-interview-questions/</link>
                <guid isPermaLink="false">671aeea7c5f85f75b4b243e8</guid>
                
                    <category>
                        <![CDATA[ Object Oriented Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ interview questions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Fri, 25 Oct 2024 01:04:39 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729679277832/18ab2f5b-0636-44e7-b063-0773e5039fb0.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>OOP is a crucial concept that every developer should grasp, especially when getting ready for job interviews. It helps you organize code into modular and reusable sections, which simplifies the development, maintenance, and scaling of software applications.</p>
<p>In this article, I'll use some common interview questions to simplify the key OOP concepts, providing clear explanations and code snippets to boost your confidence for your next interview.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-oop">What is OOP?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-are-the-four-main-principles-of-oop">What are the Four Main Principles of OOP</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-method-overloading">What is Method Overloading?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-a-constructor-in-oop">What is a Constructor in OOP?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-a-destructor-in-oop">What is a Destructor in OOP?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-a-class-in-oop">What is a Class in OOP?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-an-object-in-oop">What is an Object in OOP?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-a-static-method">What is a Static Method?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-the-difference-between-a-class-variable-and-an-instance-variable">What is the Difference Between a Class Variable and an Instance Variable?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-does-python-support-multiple-inheritance">Does Python Support Multiple Inheritance?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-the-difference-between-an-abstract-class-and-an-interface">What is the Difference Between an Abstract Class and an Interface?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-what-is-oop">What is OOP?</h2>
<p>Object-Oriented Programming (OOP) is a way of writing software that revolves around objects<strong>.</strong> These objects can store data and their actions (methods). Rather than concentrating solely on processes and logic, OOP encourages you to structure your code around these objects.</p>
<p>This approach makes it easier to create modular, reusable, and scalable software designs.</p>
<h2 id="heading-what-are-the-four-main-principles-of-oop">What are the Four Main Principles of OOP</h2>
<p>The four pillars of OOP are:</p>
<ul>
<li><p>Encapsulation</p>
</li>
<li><p>Abstraction</p>
</li>
<li><p>Inheritance</p>
</li>
<li><p>Polymorphism</p>
</li>
</ul>
<h3 id="heading-what-is-encapsulation-and-why-is-it-important">What is Encapsulation, and Why is it Important?</h3>
<p>Encapsulation helps protect the data inside an object. Think of it like keeping certain details private, allowing only controlled access to them.</p>
<p>That is, instead of directly changing or viewing the data, you interact with it through specific methods. This ensures that the data is safe from unintended changes.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, age</span>):</span>
        self.name = name
        self.__age = age  <span class="hljs-comment"># Private attribute (notice the double underscore)</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_age</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.__age  <span class="hljs-comment"># A method to access the private age attribute</span>
</code></pre>
<p>In this example, <code>__age</code> is kept private, and we can only get the age using the <code>get_age()</code> method. This ensures that <code>age</code> is not accidentally modified in a way that could cause issues.</p>
<h3 id="heading-what-is-abstraction-and-how-is-it-different-from-encapsulation">What is Abstraction, and How is it Different from Encapsulation?</h3>
<p>Abstraction allows you to show only the important details of an object or system, while hiding the complex parts that the user doesn't need to see.</p>
<p>Think of it like driving a car, you only need to know how to use the steering wheel, gear, gas pedal, and brakes to drive. You don’t need to understand how the engine works internally.</p>
<p>In programming, abstraction helps you focus on what something does, not how it works inside.</p>
<p><strong>Example:</strong> Let’s say you’re using a <code>Car</code> class. The abstraction lets you start the car without knowing all the mechanical details:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_engine</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Engine started"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">drive</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Car is driving"</span>)

<span class="hljs-comment"># The user interacts with the car without knowing how the engine works</span>
my_car = Car()
my_car.start_engine()
my_car.drive()
</code></pre>
<p>Here, you don’t need to worry about how the <code>start_engine</code> method works internally, you just use it!</p>
<p>Key Difference:</p>
<ul>
<li><p>Encapsulation: Focuses on bundling data and restricting access.</p>
</li>
<li><p>Abstraction: Focuses on hiding complexity and exposing only necessary details.</p>
</li>
</ul>
<h3 id="heading-what-is-inheritance-in-oop">What is inheritance in OOP?</h3>
<p>Inheritance lets you create a new class by using an existing class. The new class (called the child class) gets all the attributes and methods from the existing class (called the parent class).</p>
<p>This allows you to reuse code and build upon what you've already written without starting from scratch.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Parent class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand</span>):</span>
        self.brand = brand  <span class="hljs-comment"># This is an attribute (brand)</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"<span class="hljs-subst">{self.brand}</span> vehicle started"</span>)  <span class="hljs-comment"># This is a method</span>

<span class="hljs-comment"># Child class that inherits from Vehicle</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand, model</span>):</span>
        super().__init__(brand)  <span class="hljs-comment"># Inherit the brand from Vehicle (parent class)</span>
        self.model = model  <span class="hljs-comment"># Add a new attribute specific to Car</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display_info</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Car: <span class="hljs-subst">{self.brand}</span>, Model: <span class="hljs-subst">{self.model}</span>"</span>)

<span class="hljs-comment"># Creating an object of the Car class</span>
my_car = Car(<span class="hljs-string">"IVM"</span>, <span class="hljs-string">"Ikenga"</span>)
my_car.start()  <span class="hljs-comment"># Output: IVM vehicle started</span>
my_car.display_info()  <span class="hljs-comment"># Output: Car: IVM, Model: Ikenga</span>
</code></pre>
<p>In this example, <code>Vehicle</code> is the parent class, and <code>Car</code> is the child class. <code>Car</code> inherits the <code>brand</code> and <code>start()</code> method from <code>Vehicle</code>, but it also has its own attribute (<code>model</code>) and method (<code>display_info()</code>).</p>
<p>Inheritance makes it easier to create more specialized classes (like <code>Car</code>) based on a general class (like <code>Vehicle</code>).</p>
<h3 id="heading-what-is-polymorphism">What is Polymorphism?</h3>
<p>Polymorphism allows different types of objects to respond to the same action in their own unique way. It’s like how both cats and dogs make sounds, but each makes a different sound when you ask them to!</p>
<p>Polymorphism can be achieved through method overriding (when a child class has a method with the same name as a method in its parent class but provides its own implementation).</p>
<p>Example of method overriding:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Parent class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Some generic animal sound"</span>

<span class="hljs-comment"># Child class Dog overriding the sound method</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Bark"</span>

<span class="hljs-comment"># Child class Cat overriding the sound method</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Meow"</span>

<span class="hljs-comment"># Creating instances of each class</span>
my_dog = Dog()
my_cat = Cat()

<span class="hljs-comment"># Calling the sound method</span>
print(my_dog.sound())  <span class="hljs-comment"># Output: Bark</span>
print(my_cat.sound())  <span class="hljs-comment"># Output: Meow</span>
</code></pre>
<p>In this example:</p>
<ul>
<li><p><code>Animal</code> is the parent class, and it has a method called <code>sound()</code>.</p>
</li>
<li><p>Both <code>Dog</code> and <code>Cat</code> are child classes of <code>Animal</code>, and they override the <code>sound()</code> method to provide their own specific sound.</p>
</li>
<li><p>When you call <code>sound()</code> on a dog, it returns "Bark", and for a cat, it returns "Meow."</p>
</li>
</ul>
<p>This is polymorphism in action, different objects (Dog, Cat) responding to the same method (<code>sound()</code>) in different ways.</p>
<p>It's a powerful tool that helps in creating flexible and easy-to-maintain code!</p>
<h2 id="heading-what-is-method-overloading">What is Method Overloading?</h2>
<p>Method overloading happens when you create multiple methods with the same name, but with different types of parameters inside the same class.</p>
<p>While Python doesn't support traditional method overloading, you can mimic similar behavior by using default arguments/parameters or handling multiple arguments inside the method.</p>
<p>Example 1: Using Default Parameters</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Calculator</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">self, a, b=<span class="hljs-number">0</span>, c=<span class="hljs-number">0</span></span>):</span>
        <span class="hljs-keyword">return</span> a + b + c

<span class="hljs-comment"># Create an instance of Calculator</span>
calc = Calculator()

<span class="hljs-comment"># Call the add method with different numbers of arguments</span>
print(calc.add(<span class="hljs-number">5</span>))        <span class="hljs-comment"># Output: 5 (a = 5, b and c default to 0)</span>
print(calc.add(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>))    <span class="hljs-comment"># Output: 15 (a = 5, b = 10, c default to 0)</span>
print(calc.add(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span>))<span class="hljs-comment"># Output: 30 (a = 5, b = 10, c = 15)</span>
</code></pre>
<p>In this example, the <code>add</code> method has one required parameter (<code>a</code>) and two optional parameters (<code>b</code> and <code>c</code>) with default values of <code>0</code>.</p>
<p>By changing the number of arguments you pass when calling the method, you can achieve a method overloading effect.</p>
<p>Example 2: Using <code>*args</code> for Dynamic Parameters</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Calculator</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">self, *args</span>):</span>
        <span class="hljs-keyword">return</span> sum(args)

<span class="hljs-comment"># Create an instance of Calculator</span>
calc = Calculator()

<span class="hljs-comment"># Call the add method with different numbers of arguments</span>
print(calc.add(<span class="hljs-number">5</span>))           <span class="hljs-comment"># Output: 5 (adds just one number)</span>
print(calc.add(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>))       <span class="hljs-comment"># Output: 15 (adds two numbers)</span>
print(calc.add(<span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span>))   <span class="hljs-comment"># Output: 30 (adds three numbers)</span>
</code></pre>
<p>In this example, the <code>add</code> method can handle any number of arguments thanks to <code>*args</code>, allowing you to call the method with one or more parameters. It sums up all the numbers passed to it.</p>
<h2 id="heading-what-is-a-constructor-in-oop">What is a Constructor in OOP?</h2>
<p>A constructor is a special method that automatically runs when you create a new object from a class. It helps to set up the object's initial values (like setting the name or age of a person).</p>
<p>In Python, the constructor method is named <code>__init__</code>, which stands for "initialize”.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, grade</span>):</span>  <span class="hljs-comment"># The constructor method</span>
        self.name = name  <span class="hljs-comment"># Setting the name when the object is created</span>
        self.grade = grade  <span class="hljs-comment"># Setting the grade when the object is created</span>

<span class="hljs-comment"># Creating a new Student object</span>
student1 = Student(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"A"</span>)

<span class="hljs-comment"># Accessing the student's details</span>
print(student1.name)  <span class="hljs-comment"># Output: Alice</span>
print(student1.grade)  <span class="hljs-comment"># Output: A</span>
</code></pre>
<p>In this example, the <code>__init__</code> method automatically assigns the values for <code>name</code> and <code>grade</code> when we create a <code>Student</code> object (like <code>student1</code>).When you print <code>student1.name</code>, it shows "Alice," and <code>student1.grade</code> shows "A."</p>
<p>This helps to set up each student object with different details when needed!</p>
<h2 id="heading-what-is-a-destructor-in-oop">What is a Destructor in OOP?</h2>
<p>A destructor is a method that is called when an object is destroyed. In Python, the destructor is defined using <code>__del__</code>.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Demo</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Constructor called"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__del__</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Destructor called"</span>)

obj = Demo()
<span class="hljs-keyword">del</span> obj  <span class="hljs-comment"># Explicitly calling the destructor</span>
</code></pre>
<p>In this example, the <code>Demo</code> class has a constructor (<code>__init__</code>) that prints "Constructor called" when an object is created, and a destructor (<code>__del__</code>) that prints "Destructor called" when the object is deleted.</p>
<p>The <code>del obj</code> explicitly triggers the destructor to clean up the object.</p>
<h2 id="heading-what-is-a-class-in-oop">What is a Class in OOP?</h2>
<p>A class is like a template for making objects in programming. It outlines what properties (called attributes) and actions (called methods) the objects will have. Think of a class as a recipe that tells you how to create something, like a car.</p>
<p>Example:</p>
<pre><code class="lang-python"> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, make, model</span>):</span>
        self.make = make  <span class="hljs-comment"># The brand of the car, like IVM</span>
        self.model = model  <span class="hljs-comment"># The specific model, like Ikenga</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display_info</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">f"Car: <span class="hljs-subst">{self.make}</span>, Model: <span class="hljs-subst">{self.model}</span>"</span>  <span class="hljs-comment"># Shows the car's information</span>
</code></pre>
<p>In this example:</p>
<ul>
<li><p><code>Car</code> is the class that describes what a car is.</p>
</li>
<li><p><code>make</code> and <code>model</code> are attributes that hold information about the car.</p>
</li>
<li><p><code>display_info</code> is a method that tells us how to get details about the car.</p>
</li>
</ul>
<p>When we create a car object from this class, it will have its own make and model, just like real cars do!</p>
<h2 id="heading-what-is-an-object-in-oop">What is an Object in OOP?</h2>
<p>An object is a specific example of a class. Think of it like a real-life item that has certain characteristics defined by the class. When you create an object, you're giving it actual values for its properties.</p>
<pre><code class="lang-python">my_car = Car(<span class="hljs-string">"IVM"</span>, <span class="hljs-string">"Ikenga"</span>)
print(my_car.display_info())  <span class="hljs-comment"># This will show: Car: IVM, Model: Ikenga</span>
</code></pre>
<p>In this example, <code>my_car</code> is an object created from the <code>Car</code> class.</p>
<h2 id="heading-what-is-a-static-method">What is a Static Method?</h2>
<p>This is a method that belongs to a class, not to an instance (object) of that class. Unlike other methods, static methods don’t need access to instance-specific data (attributes) or class-specific data.</p>
<p>You can call a static method directly from the class without creating an object.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MathOperations</span>:</span>
<span class="hljs-meta">    @staticmethod  # This tells Python it's a static method</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>):</span>
        <span class="hljs-keyword">return</span> a + b

<span class="hljs-comment"># We don't need to create an object of the class to use the static method</span>
result = MathOperations.add(<span class="hljs-number">5</span>, <span class="hljs-number">3</span>)
print(result)  <span class="hljs-comment"># Output: 8</span>
</code></pre>
<p>In this example, <code>@staticmethod</code> is used to define the method as static. You can call <code>MathOperations.add()</code> directly using the class name, without creating an object of <code>MathOperations</code>.</p>
<h2 id="heading-what-is-the-difference-between-a-class-variable-and-an-instance-variable">What is the Difference Between a Class Variable and an Instance Variable?</h2>
<p>A class variable is shared among all instances of a class, while an instance variable is specific to each object and defined inside methods, usually within the constructor.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span>:</span>
    class_var = <span class="hljs-string">"I am a class variable"</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, instance_var</span>):</span>
        self.instance_var = instance_var  <span class="hljs-comment"># Instance variable</span>
</code></pre>
<p>In this example, a class <code>MyClass</code> has a class variable <code>class_var</code> that is shared by all instances, and an instance variable <code>instance_var</code> that is unique to each object created from the class.</p>
<h2 id="heading-does-python-support-multiple-inheritance">Does Python Support Multiple Inheritance?</h2>
<p>Yes, Python allows multiple inheritance, where a class can inherit from more than one parent class.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent1</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Parent1"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent2</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Parent2"</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span>(<span class="hljs-params">Parent1, Parent2</span>):</span>
    <span class="hljs-keyword">pass</span>

child = Child()
child.display()  <span class="hljs-comment"># Method resolution order determines which display() is called</span>
</code></pre>
<p>In this example, the <code>Child</code> class inherits from both <code>Parent1</code> and <code>Parent2</code>, and due to the <a target="_blank" href="https://docs.python.org/3/howto/mro.html">method resolution order (MRO)</a>, <code>Child</code> will call <code>Parent1</code>'s <code>display()</code> method first.</p>
<h2 id="heading-what-is-the-difference-between-an-abstract-class-and-an-interface">What is the Difference Between an Abstract Class and an Interface?</h2>
<p>An abstract class is a special type of class that you cannot create an object from. It can have both incomplete methods (called abstract methods) that don’t have any implementation, as well as fully implemented methods that do have code.</p>
<p>An interface is like a contract that defines methods that must be implemented by any class that uses it. In Python, we achieve interfaces through abstract base classes (ABCs), which only contain abstract methods. They don’t have any implementation.</p>
<p>Simple example to illustrate the concepts:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> abc <span class="hljs-keyword">import</span> ABC, abstractmethod

<span class="hljs-comment"># Abstract Class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>(<span class="hljs-params">ABC</span>):</span>
<span class="hljs-meta">    @abstractmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">pass</span>  <span class="hljs-comment"># This is an abstract method, no implementation</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sleep</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Sleeping..."</span>  <span class="hljs-comment"># This is a regular method with implementation</span>

<span class="hljs-comment"># Subclass that implements the abstract method</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Bark"</span>  <span class="hljs-comment"># Implementation of the abstract method</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sound</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Meow"</span>  <span class="hljs-comment"># Another implementation of the abstract method</span>

<span class="hljs-comment"># Using the classes</span>
my_dog = Dog()
print(my_dog.sound())  <span class="hljs-comment"># Output: Bark</span>
print(my_dog.sleep())  <span class="hljs-comment"># Output: Sleeping...</span>

my_cat = Cat()
print(my_cat.sound())  <span class="hljs-comment"># Output: Meow</span>
print(my_cat.sleep())  <span class="hljs-comment"># Output: Sleeping...</span>
</code></pre>
<p>In this example, an abstract class <code>Animal</code> with an abstract method <code>sound</code>, and two subclasses, <code>Dog</code> and <code>Cat</code>, implement the <code>sound</code> method, demonstrating the use of abstract classes and method overriding in Python.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Understanding these OOP principles is crucial for any developer. It forms the foundation of most modern programming languages.</p>
<p>By mastering the key concepts and being prepared for interview questions, you’ll not only build better software but also enhance your chances of landing your next developer role.</p>
<p>If you found this guide helpful please give it a like. You can follow me on <a target="_blank" href="https://x.com/casweb_dev">X</a> for more insightful articles.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ HTML for Beginners – HTML Basics With Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ Welcome to the exciting world of web development! In this beginner's guide, you will learn the fundamentals of HTML, the backbone of every web page. Imagine a tree: its roots anchor and nourish the entire plant. Similarly, HTML, the root of web devel... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/introduction-to-html-basics/</link>
                <guid isPermaLink="false">66d45e043a8352b6c5a2aa19</guid>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Tue, 07 May 2024 19:45:50 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/05/cover-img.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Welcome to the exciting world of web development! In this beginner's guide, you will learn the fundamentals of HTML, the backbone of every web page.</p>
<p>Imagine a tree: its roots anchor and nourish the entire plant. Similarly, HTML, the root of web development, provides the foundation for every webpage.</p>
<p>Understanding HTML's role is like grasping a tree's roots, it forms the fundamental basis for comprehending how web pages come to life.</p>
<p>By the end of this tutorial, you'll be equipped with the knowledge to kick-start your coding journey.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-html">What is HTML?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-basic-structure-of-an-html-document">Basic Structure of an HTML Document</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-comments">Comments</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-tags-and-elements">Tags and Elements</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-html-attributes">HTML Attributes</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-html-multimedia">HTML Multimedia</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-best-practices">Best Practices</a></p>
</li>
</ul>
<h2 id="heading-what-is-html">What is HTML?</h2>
<p>HTML, which stands for Hypertext Markup Language, is the standard language used for creating and designing the structure of a web page. It allows you to organize content on your website, define its structure, and establish the relationships between different elements.</p>
<h2 id="heading-basic-structure-of-an-html-document">Basic Structure of an HTML Document</h2>
<p>An HTML document follows a specific structure that acts as the foundation for your web page:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- your web page content goes here --&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Let's break it down:</p>
<p><code>&lt;!DOCTYPE html&gt;</code>: defines the document type and version of HTML being used (HTML5 in this case).</p>
<p><code>&lt;html lang="en"&gt;</code> and <code>&lt;/html&gt;</code>: opening and closing tag of the root element that wraps the entire HTML content. The attribute <code>lang="en"</code> defines the language (in this case, USA English)</p>
<p><code>&lt;head&gt;</code> and <code>&lt;/head&gt;</code>: opening and closing tag of the <code>head</code> element contains meta-information <code>&lt;meta &gt;</code> about the HTML document, the page title <code>&lt;title&gt;&lt;/title&gt;</code> you see in the browser tab, and link <code>&lt;link /&gt;</code> which defines a link between your HTML document and an external resources, like stylesheet, favicon, import and so on.</p>
<p><code>&lt;body&gt;</code> and <code>&lt;/body&gt;</code> : opening and closing <code>body</code> tag encloses all the visible content of a web page, including text, images, links, forms, and other elements that users interact with.</p>
<p><strong>Note</strong>: All HTML elements have opening (<code>**&lt; &gt;**</code>) and closing (<code>**&lt;/ &gt;**</code>) tags, except for self-closing (<code>**&lt; &gt;**</code> or <code>**&lt; /&gt;**</code>) tags, which I will explain in more detail later.</p>
<h2 id="heading-comments">Comments</h2>
<p>Notice this <code>&lt;!-- your web page content goes here --&gt;</code> in the above html basic structure, it's called comments. Comments are used to add explanatory notes that are not displayed when the web page is viewed in a browser. They are useful for documenting your code, providing information to other developers, or temporarily excluding specific parts of the code. You can create comment using this tag <code>&lt;!--</code> <code>your comment goes here</code> <code>--&gt;</code>.</p>
<p>There are:</p>
<ol>
<li><p><strong>Single-line commen</strong>t: <code>&lt;!-- This is a single-line comment --&gt;</code></p>
</li>
<li><p><strong>Multi-line comment</strong>: <code>&lt;!-- This is a multi-line comment. It can span multiple lines. All content within the comment block will be ignored by the browser. --&gt;</code></p>
</li>
</ol>
<h2 id="heading-tags-and-elements">Tags and Elements</h2>
<p>HTML uses tags to define different elements on a webpage. Tags are enclosed in angle brackets (<code>&lt; &gt;</code>). There are opening (<code>&lt; &gt;</code>) and closing (<code>&lt;/ &gt;</code>) tags, and self-closing (<code>&lt; &gt;</code> or <code>&lt; /&gt;</code>) tag. Here are a few examples:</p>
<h3 id="heading-headings">Headings</h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>This is a Heading 1<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>This is a Heading 2<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
<span class="hljs-comment">&lt;!-- ... up to &lt;h6&gt; --&gt;</span>
</code></pre>
<p>The heading tags <code>&lt;h1&gt;</code> to <code>&lt;h6&gt;</code> are used to define headings or subheadings within a document. These tags represent a hierarchy of headings, with <code>&lt;h1&gt;</code> being the highest level (main heading) and <code>&lt;h6&gt;</code> being the lowest level (lowest subheading level).</p>
<p>Its purpose is to structure and organize the content of a web page, making it more readable and accessible.</p>
<h3 id="heading-paragraph">Paragraph</h3>
<p>The paragraph tag (<code>&lt;p&gt; your text goes here &lt;/p&gt;</code>) is used to separate blocks of text into distinct paragraphs. It is a block-level element that represents a unit of text or a block of content, and it is commonly used to structure and separate text on a webpage.</p>
<p>The <code>&lt;p&gt;</code> tag is part of the structural markup in HTML and helps in creating well-organized and readable content.</p>
<h3 id="heading-line-breaks">Line Breaks</h3>
<p>To create a line break without starting a new paragraph, use the break (<code>&lt;br&gt;</code>) tag.</p>
<p>Example 1 - Basic Line Break:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is the first line.<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>This is the second line.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<p>This is the first line.<br>This is the second line.</p>
<p>Example 2 - Line Breaks in Text:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This text contains a<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>line break.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<p>This text contains a<br>line break.</p>
<p>Example 3 - Line Breaks in List:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 2<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>with a line break<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<ul>
<li><p>Item 1</p>
</li>
<li><p>Item 2<br>  with a line break</p>
</li>
<li><p>Item 3</p>
</li>
</ul>
<p>Example 4 - Line Breaks in Address:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">address</span>&gt;</span>
  Nuel Cas<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
  23 Musa Yar'Dua VI<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
  Lagos, Nigeria
<span class="hljs-tag">&lt;/<span class="hljs-name">address</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<p>Nuel Cas<br>23 Musa Yar'Dua VI<br>Lagos, Nigeria</p>
<p>Example 5: Line Breaks with Multiple<br>Tags</p>
<pre><code class="lang-python">&lt;p&gt;This <span class="hljs-keyword">is</span> a paragraph <span class="hljs-keyword">with</span>&lt;br&gt;&lt;br&gt;multiple line breaks.&lt;/p&gt;
</code></pre>
<p>This will render as:</p>
<p>This is a paragraph with</p>
<p>multiple line breaks.</p>
<p>While break (<code>&lt;br&gt;</code>) tag is commonly used for simple line breaks, CSS and block-level elements like <code>&lt;p&gt;</code> and <code>&lt;div&gt;</code> tags are often preferred for more complex layouts.</p>
<p>Overusing <code>&lt;br&gt;</code> tags for layout purposes is discouraged. CSS is generally more suitable for controlling the spacing and layout of elements on a webpage.</p>
<h3 id="heading-div">Div</h3>
<p>A <code>&lt;div&gt;</code> tag, which stands for "division" is one of the most commonly used container elements in HTML. It is a block-level container that is used to group other HTML elements together and apply styles or scripting to them collectively.</p>
<p>Here's an example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph inside a div.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>List item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>List item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>In this example, the <code>&lt;div&gt;</code> element wraps around a paragraph (<code>&lt;p&gt;</code>) and an unordered list (<code>&lt;ul&gt;</code>). This grouping allows you to apply styles or manipulate these elements together using CSS or JavaScript.</p>
<p><strong>Note</strong>: The <code>&lt;div&gt;</code> tag is often used for layout purposes, helping structure the content of a webpage. For more semantic and specific meanings, HTML5 introduced new semantic tags like <code>&lt;section&gt;</code>, <code>&lt;article&gt;</code>, <code>&lt;header&gt;</code>, <code>&lt;footer&gt;</code>, and so on, which provide better clarity about the content's purpose.</p>
<h3 id="heading-semantic-tags">Semantic Tags</h3>
<p>They are like special labels that tell web browsers and developers what different parts of a webpage are all about. They help make websites easier to understand for both people and computers.</p>
<p>By using these tags, you can make your websites more accessible and easier to find on search engines. Here are some common HTML semantic tags along with examples:</p>
<ol>
<li><code>&lt;header&gt;</code>: The header tag represents introductory content at the beginning of a section or webpage. It typically contains logos, navigation menus, and other introductory elements.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Website Title<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
</code></pre>
<ol start="2">
<li><code>&lt;nav&gt;</code>: Use nav tag to define navigation links within your webpage. It contains links to other pages or sections of the website.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Contact<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
</code></pre>
<ol start="3">
<li><code>&lt;main&gt;</code>: Used to define the main content of a webpage. It helps improve the accessibility and structure of your HTML code, as it clearly identifies the main content area for screen readers and other assistive technologies. It also helps search engines understand the relevance of the content on your page, which can improve your website's Search Engine Optimization (SEO).</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">article</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Page Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Page content goes here...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">article</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
</code></pre>
<ol start="4">
<li><code>&lt;section&gt;</code>: Use the <code>section</code> tag when you want to define sections within a webpage. Also, for grouping related content together.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Section Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Section content goes here...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<ol start="5">
<li><code>&lt;article&gt;</code>: Use the <code>article</code> tag when you want to define an independent piece of content that can stand alone, such as a blog post, news article, or forum post.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">article</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Article Title<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Article content goes here...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">article</span>&gt;</span>
</code></pre>
<ol start="6">
<li><code>&lt;aside&gt;</code>: Use the <code>aside</code> tag when you want to define content that is related to the main content but not part of it, such as sidebars, advertisements, or related links.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Related Links<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Link 1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Link 2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#"</span>&gt;</span>Link 3<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
</code></pre>
<ol start="7">
<li><code>&lt;footer&gt;</code>: Used to define the footer of a webpage, typically containing copyright information, contact details, or links to related pages.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-symbol">&amp;copy;</span> Nuel Cas Website<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
</code></pre>
<h3 id="heading-list-tag">List Tag</h3>
<p>Lists <code>&lt;li&gt;</code> allow you to organize and structure content in a hierarchical manner. They are two types: ordered <code>&lt;ol&gt;</code> (numbered) and unordered (<code>&lt;ul&gt;</code>) (bulleted) lists.</p>
<p>Ordered List: Use <code>&lt;ol&gt;</code> for ordered lists, and <code>&lt;li&gt;</code> for list items.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>First item<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Second item<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Third item<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<ol>
<li><p>First item</p>
</li>
<li><p>Second item</p>
</li>
<li><p>Third item</p>
</li>
</ol>
<p>Unordered List: The <code>&lt;ul&gt;</code> tag is used to create an unordered list, where the order of the items doesn't matter, it renders bulleted items. Each item in the list is represented by the <code>&lt;li&gt;</code> (list item) tag.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<ul>
<li><p>Item 1</p>
</li>
<li><p>Item 2</p>
</li>
<li><p>Item 3</p>
</li>
</ul>
<p>Lists can be nested within each other. For example, you can have an ordered list within an unordered list or vice versa.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Unordered List Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Unordered List Item 2
    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Ordered List Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Ordered List Item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Unordered List Item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<ul>
<li><p>Unordered List Item 1</p>
</li>
<li><p>Unordered List Item 2</p>
</li>
</ul>
<ol>
<li><p>Ordered List Item 1</p>
</li>
<li><p>Ordered List Item 2</p>
</li>
</ol>
<ul>
<li>Unordered List Item 3</li>
</ul>
<p>List items can also have attributes. For example, you might use the <code>type</code> attribute with the <code>&lt;ol&gt;</code> tag to change the numbering style.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ol</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"A"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item 3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<p>A. Item 1<br>B. Item 2<br>C. Item 3</p>
<p>The <code>&lt;ul&gt;</code>, <code>&lt;ol&gt;</code>, and <code>&lt;li&gt;</code> tags in HTML are essential for creating organized lists and structuring content on web pages. They provide flexibility in presenting information in both ordered and unordered formats.</p>
<h3 id="heading-span-tag">Span Tag</h3>
<p>The <code>&lt;span&gt;</code> tag is a generic inline (it does not create a line break) container used to group and apply styles to inline elements or text. It is typically used when you want to apply styles or using JavaScript to manipulate specific portions of text within a larger block of content without affecting the overall structure.</p>
<p>Here's an example of how the <code>&lt;span&gt;</code> tag can be used in HTML:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"color: red; font-weight: bold;"</span>&gt;</span>highlighted<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> text.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<p>In this example, the word "highlighted" will be displayed in red and bold, as specified by the inline styles applied to the <code>&lt;span&gt;</code> element.</p>
<h3 id="heading-links">Links</h3>
<p>The <code>&lt;link&gt;</code> tag is used to define a link between a document and an external resource. Its primary purpose is to include external resources, such as stylesheets, icons, and other documents. Let's look at the common use cases of the <code>&lt;link&gt;</code> tag:</p>
<p><strong>Linking stylesheet</strong>: The most common use of the <code>&lt;link&gt;</code> tag is to link an external CSS (Cascading Style Sheets) file to an HTML document. This allows you to separate the styling of your website from its structure, making it easier to maintain and update.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
</code></pre>
<p>In this example, the <code>rel</code> attribute specifies the relationship between the HTML document and the linked resource (stylesheet), the <code>type</code> attribute indicates the type of the linked resource (<code>text/css</code> for stylesheets), and the <code>href</code> attribute specifies the path to the external CSS file.</p>
<p><strong>Note</strong>: Linking a CSS file should be done inside the <code>&lt;head&gt;</code> element.</p>
<p><strong>Linking icon</strong>: The <code>&lt;link&gt;</code> tag is also commonly used to link the favicon icon for a webpage, which is the small icon that appears in the browser tab or next to the URL in the address bar.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"icon"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"favicon.ico"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"image/x-icon"</span>&gt;</span>
</code></pre>
<p>In this case, the <code>rel</code> attribute is set to "icon" to indicate that it is an icon file, and the <code>href</code> attribute specifies the path to the icon file. The <code>type</code> attribute indicates the type of the linked file, in this case, <code>image/x-icon</code> for icons.</p>
<p><strong>Linking external documents</strong>: The <code>&lt;link&gt;</code> tag can be used to link other external documents, such as:</p>
<ol>
<li>Stylesheet for printing: Imagine you have a special design for when someone wants to print your webpage. The <code>&lt;link&gt;</code> tag can connect your webpage to a separate stylesheet designed just for printing. This way, when someone prints your page, it looks nice and tidy.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- Link to the stylesheet for printing --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"print-styles.css"</span> <span class="hljs-attr">media</span>=<span class="hljs-string">"print"</span>&gt;</span>
</code></pre>
<ol start="2">
<li>Alternative versions of a document (like translations): Sometimes, you might have different versions of your webpage for different languages or purposes. The <code>&lt;link&gt;</code> tag can connect your webpage to these alternative versions.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"alternate"</span> <span class="hljs-attr">hreflang</span>=<span class="hljs-string">"es"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"spanish-version.html"</span>&gt;</span>
</code></pre>
<ol start="3">
<li>Feeds for content syndication: Let's say you have a blog, and you want others to easily see your latest posts. The <code>&lt;link&gt;</code> tag can help by connecting your webpage to a feed, which is like a stream of your latest content.</li>
</ol>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"alternate"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"application/rss+xml"</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"RSS Feed"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"rss-feed.xml"</span>&gt;</span>
</code></pre>
<p>The <code>&lt;link&gt;</code> tag is like a connector that helps your webpage interact with other files on the internet.</p>
<h3 id="heading-anchor-tag">Anchor Tag</h3>
<p>The anchor tag, represented by <code>&lt;a&gt;</code>, is used to create hyperlinks or anchor points within a webpage. It is primarily used to define a hyperlink, allowing users to navigate to another webpage, a different section of the same page, or even an external resource.</p>
<p>The anchor tag uses the <code>href</code> attribute to specify the destination URL (Uniform Resource Locator).</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://www.example.com"</span>&gt;</span>Visit Example.com<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<h3 id="heading-form-tag">Form tag</h3>
<p>HTML forms are essential for user interaction on websites. They allow users to input data that can be sent to a server for processing. The basic structure of an HTML form involves several key elements:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- Your form elements go here --&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>The <code>&lt;form&gt;</code> tag marks the beginning and end of your form. It acts as a container for various form elements. It commonly houses label, input types, textarea, and button tags.</p>
<h4 id="heading-label">Label</h4>
<p>The <code>&lt;label&gt;</code> tag is used to define a label for an input element. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"username"</span>&gt;</span>Username:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
</code></pre>
<h4 id="heading-input-type">Input type</h4>
<p>In a form, different input types serve various purposes. The input (<code>&lt;input&gt;</code>) tag defines an interactive element for users to enter data. Commonly used ones are:</p>
<p>Text Input:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your username"</span>&gt;</span>
</code></pre>
<p>Password Input:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your password"</span>&gt;</span>
</code></pre>
<p>Radio Buttons:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"gender"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"male"</span>&gt;</span> Male
<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"gender"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"female"</span>&gt;</span> Female
</code></pre>
<p>Checkboxes:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"checkbox"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"subscribe"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"yes"</span>&gt;</span> Subscribe to newsletter
</code></pre>
<h4 id="heading-textarea">Textarea</h4>
<p>The <code>&lt;textarea&gt;</code> tag defines a multi-line text input control, commonly used within form elements. Example:</p>
<pre><code class="lang-python">&lt;textarea name=<span class="hljs-string">"message"</span> placeholder=<span class="hljs-string">"Enter your message"</span>&gt;&lt;/textarea&gt;
</code></pre>
<h4 id="heading-button-for-submitting-forms">Button (for submitting forms)</h4>
<p>The most crucial part of a form is allowing users to submit their input. For this, you can use a button (<code>&lt;button&gt;</code>) tag to submit:</p>
<p>Example:</p>
<pre><code class="lang-python">&lt;button type=<span class="hljs-string">"submit"</span>&gt;Submit&lt;/button&gt;
</code></pre>
<p>The <code>&lt;button&gt;</code> tag creates a clickable button. The <code>type="submit"</code> attribute indicates that clicking this button will submit the form.</p>
<h3 id="heading-quick-tips">Quick Tips</h3>
<ul>
<li><p>Always include a <code>name</code> attribute in your form elements. It helps identify and process the data on the server.</p>
</li>
<li><p>Use the <code>placeholder</code> attribute to give users a hint about the expected input.</p>
</li>
<li><p>Consider the user experience when choosing input types. For instance, use radio buttons for mutually exclusive options.</p>
</li>
</ul>
<p>Here is a code snippet demonstrating usage of a form element:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"username"</span>&gt;</span>Username:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your username"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"password"</span>&gt;</span>Password:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your password"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span>Gender:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"gender"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"male"</span>&gt;</span> Male
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"gender"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"female"</span>&gt;</span> Female

  <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span>Subscribe to newsletter:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"checkbox"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"subscribe"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"yes"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"message"</span>&gt;</span>Your Message:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"message"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"message"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your message"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<h3 id="heading-self-closing-tags">Self-closing Tags</h3>
<p>The <code>&lt;input&gt;</code> or <code>&lt;input /&gt;</code> element above is a self-closing tag, which means it doesn't require a separate closing tag.</p>
<p>There are other self-closing tags in HTML:</p>
<ul>
<li><p>Image (<code>&lt;img&gt;</code> or <code>&lt;img /&gt;</code>).</p>
</li>
<li><p>Line breaks (<code>&lt;br&gt;</code> or <code>&lt;br /&gt;</code>).</p>
</li>
<li><p>External resource link (<code>&lt;link&gt;</code> or <code>&lt;link /&gt;</code>).</p>
</li>
<li><p>Horizontal rule (<code>&lt;hr&gt;</code> or <code>&lt;hr /&gt;</code>).</p>
</li>
<li><p>Meta data (<code>&lt;meta&gt;</code> or <code>&lt;meta /&gt;</code>).</p>
</li>
<li><p>Table column (<code>&lt;col&gt;</code> or <code>&lt;col /&gt;</code>).</p>
</li>
<li><p>Base URL for relative links (<code>&lt;base&gt;</code> or <code>&lt;base /&gt;</code>).</p>
</li>
<li><p>Word break opportunity (<code>&lt;wbr&gt;</code> or <code>&lt;wbr /&gt;</code>).</p>
</li>
<li><p>Area (<code>&lt;area&gt;</code> or <code>&lt;area /&gt;</code>) which defines an area inside an image map so the image can have a clickable region.</p>
</li>
</ul>
<p>Note: HTML5 (latest version of HTML) allows you to omit the slash (<code>/</code>) at the end of self-closing tags, but including it ensures compatibility with older standards like XHTML and some tools.</p>
<h2 id="heading-html-attributes">HTML Attributes</h2>
<p>This is an additional information or characteristics that you can apply to HTML elements to modify their behavior, appearance, or define certain properties. Attributes are always included in the opening tag of an HTML element and are written as name-value pairs.</p>
<p>The basic syntax for an HTML attribute is:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">tagname</span> <span class="hljs-attr">attribute</span>=<span class="hljs-string">"value"</span>&gt;</span>Content<span class="hljs-tag">&lt;/<span class="hljs-name">tagname</span>&gt;</span>
</code></pre>
<p>Here, <code>attribute</code> is the name of the attribute, and <code>"value"</code> is the value assigned to that attribute.</p>
<p>There are many attributes available for various HTML elements, here are few ones:</p>
<h3 id="heading-id-attribute">id Attribute</h3>
<p>It provides a unique identifier for an HTML element. It should be unique within the entire HTML document.</p>
<p>"id" attribute helps you uniquely identify and control specific elements on a webpage, just like how each student's ID number helps identify them uniquely in a school.</p>
<p>Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"header"</span>&gt;</span>This is a div with an id attribute.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-class-attribute">class Attribute</h3>
<p>Used to assign one or more class names to an HTML element. It also helps you organize and style different parts of a webpage by grouping them together.</p>
<p>Here is the syntax for class attribute:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">tagname</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"classname1 classname2 ..."</span>&gt;</span>Content<span class="hljs-tag">&lt;/<span class="hljs-name">tagname</span>&gt;</span>
</code></pre>
<p>Suppose you want to style multiple paragraphs with the same font and color. Instead of writing the same CSS styles for each paragraph individually, you can assign a common class to all those paragraphs and define the styles for that class in your CSS file. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"highlight"</span>&gt;</span>This is the first paragraph.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"highlight"</span>&gt;</span>This is the second paragraph.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"highlight"</span>&gt;</span>This is the third paragraph.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p><strong>Note</strong>: The "id" attribute and the "class" attribute in HTML serve similar purposes in that they both allow you to uniquely identify elements on a webpage. However, there are key differences between the two:</p>
<ul>
<li><p>Use the "id" attribute when you need to uniquely identify a single element.</p>
</li>
<li><p>Use the "class" attribute when you want to group multiple elements together and apply styling or functionality to them collectively.</p>
</li>
</ul>
<p>While both attributes can be used for styling, the "id" attribute is more suitable for unique styling, while the "class" attribute is ideal for applying consistent styles to multiple elements.</p>
<h3 id="heading-src-source-attribute">src (source) Attribute</h3>
<p>It specifies the source URL of an image to be displayed. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Nuel Cas Photo"</span>&gt;</span>
</code></pre>
<p><strong>Note</strong>: The <code>alt</code> attribute is used to provide alternative text for an image if the image fails to load. It serves as a descriptive text that is displayed in place of the image, helping users understand the content or purpose of the image even when it's not visible.</p>
<h3 id="heading-href-attribute-for-links">href Attribute (for links)</h3>
<p>It specifies the URL that the hyperlink points to. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://www.nuelcas.com"</span>&gt;</span>Visit Nuel Cas<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<h3 id="heading-width-and-height-attribute-for-images">width and height Attribute (for images)</h3>
<p>It determines the width and height of an image in pixels. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Nuel Cas Photo"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"300"</span>&gt;</span>
</code></pre>
<h3 id="heading-style-attribute">style Attribute</h3>
<p>Allows you to apply inline CSS styles to an HTML element. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"color: red; font-size: 18px;"</span>&gt;</span>This is a red text.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
</code></pre>
<h3 id="heading-disabled-attribute-for-form-elements">disabled Attribute (for form elements)</h3>
<p>Allows you to disable user interaction with form element. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">disabled</span>&gt;</span>
</code></pre>
<h3 id="heading-type-attribute-for-form-element-and-list-items">type Attribute (for form element and list items)</h3>
<p>You can use the <code>type</code> attribute with the <code>&lt;ol&gt;</code> tag to change the numbering style. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">ol</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"A"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item A<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item B<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Item C<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
</code></pre>
<p>This will render as:</p>
<pre><code class="lang-text">A. Item A
B. Item B
C. Item C
</code></pre>
<p>Also, you can use <code>type</code> attribute to specify the input type of form element. Say you want to notify the browser that this input is for password, use the code below</p>
<pre><code class="lang-python">&lt;input type=<span class="hljs-string">"password"</span> name=<span class="hljs-string">"password"</span> placeholder=<span class="hljs-string">"Enter your password"</span>&gt;
</code></pre>
<h3 id="heading-name-attribute-for-form-element">name attribute (for form element)</h3>
<p>The <code>name</code> attribute provides a unique identifier for each form field. When the form is submitted to the server, the data entered into each field is sent with the corresponding name as a <em>key-value</em> pair. The code snippet below shows that you want the server to identify this input as email.</p>
<pre><code class="lang-python"> &lt;input type=<span class="hljs-string">"email"</span> id=<span class="hljs-string">"email"</span> name=<span class="hljs-string">"email"</span> placeholder=<span class="hljs-string">"Enter your email"</span>&gt;
</code></pre>
<p><strong>Note</strong>: Understanding and using attributes effectively is essential for controlling the appearance and behavior of elements in your HTML documents.</p>
<h2 id="heading-html-multimedia">HTML Multimedia</h2>
<p>You may need to integrate various types of media content into web pages, such as images, audio, and video. These media elements enhance the user experience by making web content more engaging and dynamic.</p>
<p>Here are the different types of multimedia you can use in HTML:</p>
<h3 id="heading-images">Images</h3>
<p>Images are the most common type of multimedia in HTML. You can add images to a web page using the <code>&lt;img&gt;</code> tag. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"image.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Description of the image"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"200"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"150"</span>&gt;</span>
</code></pre>
<p>In the above example, <code>src</code> specifies the source URL of the image, <code>alt</code> provides alternative text for accessibility and SEO, and <code>width</code> and <code>height</code> are optional attributes to set the dimensions of the image.</p>
<h3 id="heading-audio">Audio</h3>
<p>You can embed audio files directly into a web page using the <code>&lt;audio&gt;</code> tag. This allows you to play audio clips, music, or other sound recordings. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">audio</span> <span class="hljs-attr">controls</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">source</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"audio.mp3"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"audio/mpeg"</span>&gt;</span>
  Your browser may not support the audio element.
<span class="hljs-tag">&lt;/<span class="hljs-name">audio</span>&gt;</span>
</code></pre>
<p>In the above example, <code>controls</code> provides play, pause, and volume controls for the user, <code>src</code>specifies the source URL of the audio file, while <code>type</code> specifies the <a target="_blank" href="https://en.wikipedia.org/wiki/MIME">MIME</a> (Multipurpose Internet Mail Extensions) type of the audio file.</p>
<h3 id="heading-video">Video</h3>
<p>The <code>&lt;video&gt;</code> tag is used to embed video files into a web page. This allows you to play videos within the content. Example:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">video</span> <span class="hljs-attr">controls</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"640"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"360"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">source</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"video.mp4"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"video/mp4"</span>&gt;</span>
  Your browser may not support the video element.
<span class="hljs-tag">&lt;/<span class="hljs-name">video</span>&gt;</span>
</code></pre>
<p>In the above example, <code>controls</code> provides play, pause, and volume controls for the user, <code>width</code> and <code>height</code> specifies the dimensions of the video, <code>src</code> specifies the source URL of the video file, while <code>type</code> specifies the MIME type of the video file.</p>
<h3 id="heading-iframe">iframe</h3>
<p><code>&lt;iframe&gt;</code> allows you to display content from a different source or page inside a frame on your webpage. This can be useful for embedding videos, maps, web pages, or other external content. Example using <code>&lt;iframe&gt;</code> to embed a video from YouTube:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">iframe</span> 
  <span class="hljs-attr">src</span>=<span class="hljs-string">"https://www.youtube.com/embed/VIDEO_ID"</span> 
  <span class="hljs-attr">width</span>=<span class="hljs-string">"560"</span> 
  <span class="hljs-attr">height</span>=<span class="hljs-string">"315"</span> 
  <span class="hljs-attr">title</span>=<span class="hljs-string">"YouTube Video"</span> 
  <span class="hljs-attr">frameborder</span>=<span class="hljs-string">"0"</span> 
  <span class="hljs-attr">allowfullscreen</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">iframe</span>&gt;</span>
</code></pre>
<p>In the above code snippet, <code>src</code> attribute specifies the URL of the page or content you want to embed. Sizes are controlled using the <code>width</code> and <code>height</code> attributes. <code>title</code> attribute provides a description for the content, which is important for accessibility.</p>
<p>The <code>frameborder</code> attribute controls whether the iframe has a border (0 for no border, 1 for a border), while the <code>allowfullscreen</code> attribute allows the video to be played in full-screen mode.</p>
<p><strong>Note</strong>: Replace <code>"VIDEO_ID"</code> with the ID of the YouTube video you want to embed.</p>
<h2 id="heading-best-practices">Best Practices</h2>
<ol>
<li>Follow proper HTML document structure:</li>
</ol>
<ul>
<li><p>Start your HTML document with a <code>&lt;!DOCTYPE html&gt;</code> declaration to ensure browser compatibility and standards compliance.</p>
</li>
<li><p>Always include the <code>&lt;html&gt;</code>, <code>&lt;head&gt;</code>, and <code>&lt;body&gt;</code> tags in your document.</p>
</li>
<li><p>Use the <code>&lt;meta charset="UTF-8"&gt;</code> tag to specify the character encoding of your document.</p>
</li>
<li><p>Define the language of your document using the language (<code>&lt;html lang="en"&gt;</code>) attribute.</p>
</li>
<li><p>Include a descriptive title (<code>&lt;title&gt;</code>) tag within the head (<code>&lt;head&gt;</code>) section to provide context for the page.</p>
</li>
</ul>
<ol start="2">
<li>Use semantic HTML element:</li>
</ol>
<ul>
<li>Utilize semantic HTML elements like <code>&lt;header&gt;</code>, <code>&lt;nav&gt;</code>, <code>&lt;main&gt;</code>, <code>&lt;section&gt;</code>, <code>&lt;article&gt;</code>, <code>&lt;aside&gt;</code>, and <code>&lt;footer&gt;</code> to provide clarity and structure to your content. Semantic elements improve accessibility, SEO, and maintainability of your code.</li>
</ul>
<ol start="3">
<li>Comment your code:</li>
</ol>
<ul>
<li>Use comments <code>&lt;!-- --&gt;</code> to document your HTML code, explaining its purpose and functionality. Comments improve code readability and facilitate collaboration among developers.</li>
</ul>
<ol start="4">
<li>Structure your content with proper tags:</li>
</ol>
<ul>
<li><p>Use heading tags <code>&lt;h1&gt;</code> to <code>&lt;h6&gt;</code> for defining the hierarchy of your content.</p>
</li>
<li><p>Utilize paragraph tags <code>&lt;p&gt;</code> to separate blocks of text into distinct paragraphs.</p>
</li>
<li><p>Employ lists (<code>&lt;ul&gt;</code>, <code>&lt;ol&gt;</code>, <code>&lt;li&gt;</code>) to organize and structure content in a hierarchical manner.</p>
</li>
</ul>
<ol start="5">
<li>Group elements with <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> sparingly:</li>
</ol>
<ul>
<li>Use <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> tags as needed to group and style elements, but avoid excessive nesting and over-reliance on these elements. Prefer more semantic alternatives where appropriate.</li>
</ul>
<ol start="6">
<li>Do not overuse line breaks (<code>&lt;br&gt;</code>):</li>
</ol>
<ul>
<li>While <code>&lt;br&gt;</code> tags can be useful for simple line breaks, avoid overusing them for layout purposes. Instead, use CSS and block-level elements for more complex layouts to maintain better code readability and maintainability.</li>
</ul>
<ol start="7">
<li>Always use alternative text for images (<code>alt</code> attribute):</li>
</ol>
<ul>
<li>Always include descriptive alternative text using the <code>alt</code> attribute for images (<code>&lt;img&gt;</code> tags). This improves accessibility for users with visual impairments and ensures that content remains understandable even if images fail to load.</li>
</ul>
<ol start="8">
<li>Optimize forms for user experience (UX):</li>
</ol>
<ul>
<li><p>Include meaningful <code>name</code> attributes for form elements to identify and process data accurately on the server.</p>
</li>
<li><p>Utilize appropriate input types (<code>type</code> attribute) for form fields to enhance user experience and ensure data validation.</p>
</li>
<li><p>Use the <code>placeholder</code> attribute to provide hints or expected input for form fields.</p>
</li>
</ul>
<ol start="9">
<li>Ensure compatibility with older browsers:</li>
</ol>
<ul>
<li><p>Your code should undergo <a target="_blank" href="https://www.freecodecamp.org/news/cross-browser-compatibility-testing-best-practices-for-web-developers/">compatibility testing</a> across different browsers and devices to ensure consistent rendering and functionality.</p>
</li>
<li><p>Include appropriate fallbacks for newer HTML features or attributes, this will help maintain compatibility with older browsers.</p>
</li>
</ul>
<ol start="10">
<li>Stay updated with HTML standards:</li>
</ol>
<ul>
<li>Keep yourself updated with the latest HTML standards and best practices to leverage new features, improve performance, and enhance the user experience of your web applications.</li>
</ul>
<p>By adhering to these best practices, you can create well-structured, accessible, and maintainable HTML code that contributes to the overall quality and usability of your web projects.</p>
<h4 id="heading-if-you-have-read-enjoyed-and-desire-more-of-this-piece-feel-free-to-reach-out-to-me-on-xhttpstwittercomcaswebdev-and-linkedinhttpswwwlinkedincomincasmir-onyekani-for-further-collaboration">If you have read, enjoyed, and desire more of this piece, feel free to reach out to me on <a target="_blank" href="https://twitter.com/casweb_dev">X</a> and <a target="_blank" href="https://www.linkedin.com/in/casmir-onyekani/">LinkedIn</a> for further collaboration.</h4>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Cross-Browser Compatibility Testing – Best Practices for Web Developers ]]>
                </title>
                <description>
                    <![CDATA[ Imagine putting in a ton of work to build a web application. And then it works in the Chrome browser, but misbehaves in Mozilla, Safari, or any other browser. As a web developer, you're likely eager to create amazing web applications that reach users... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/cross-browser-compatibility-testing-best-practices-for-web-developers/</link>
                <guid isPermaLink="false">66d45e01264384a65d5a9508</guid>
                
                    <category>
                        <![CDATA[ Browsers ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Compatibility ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Testing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Thu, 05 Oct 2023 23:40:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/10/cross-browser-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Imagine putting in a ton of work to build a web application. And then it works in the Chrome browser, but misbehaves in Mozilla, Safari, or any other browser.</p>
<p>As a web developer, you're likely eager to create amazing web applications that reach users across the globe. But one major challenge you'll face is ensuring that your web app works seamlessly on various web browsers. This is where cross-browser compatibility testing comes into play.</p>
<p>In this article, we'll dive into the challenges of cross-browser compatibility, and I'll give you some best practices to overcome them.</p>
<h2 id="heading-what-is-cross-browser-compatibility-testing">What is Cross-Browser compatibility testing?</h2>
<p>Cross-browser compatibility testing is a critical quality assurance process in web development. It involves testing and ensuring that a website or web application functions and appears consistently and correctly across different web browsers and their various versions.</p>
<p>Since web browsers are developed by different companies and have distinct rendering engines, they may interpret HTML, CSS, and JavaScript code differently. This can lead to discrepancies in how a website looks and behaves, causing issues for users who access the site using different browsers.</p>
<h2 id="heading-common-cross-browser-compatibility-issues">Common Cross-Browser Compatibility Issues</h2>
<p>One fundamental cross-browser compatibility concern relates to the rendering of web pages.</p>
<p>CSS styles add another layer of complexity. Inconsistencies in how browsers interpret and apply these styles can lead to visual disparities, such as variations in font sizes, colors, spacing, and layout. These discrepancies can undermine the website's design integrity and user experience.</p>
<p>Plugins like Flash or Java pose compatibility challenges, as not all browsers support them, and some browsers have disabled them entirely. This can result in certain features of a website not working as intended or being inaccessible to users on specific browsers.</p>
<p>Web developers often rely on third-party libraries and frameworks to streamline development. But these dependencies may not be universally compatible with all browsers.</p>
<p>Compatibility issues with these external tools can lead to malfunctions or performance bottlenecks, affecting the website's overall stability and functionality.</p>
<p>Browser-specific bugs related to form submission, caching, and other critical functions can create headaches for developers. These bugs may manifest differently on each browser, requiring meticulous testing and workarounds to ensure consistent performance and functionality across the board.</p>
<h2 id="heading-differences-between-cross-browser-and-compatibility-testing">Differences Between Cross-Browser and Compatibility Testing</h2>
<p>There are some key differences between cross-browser testing and compatibility testing. Here are the main ones:</p>
<h3 id="heading-scope">Scope</h3>
<p>Cross-browser testing focuses on ensuring that a website or web application functions consistently and correctly across different web browsers. It primarily addresses variations in rendering and behavior caused by different browsers' rendering engines.</p>
<p>Compatibility testing is a broader testing approach that encompasses not only different browsers but also various operating systems, devices, screen sizes, and network conditions. It assesses how well a website or application functions across a range of diverse environments.</p>
<h3 id="heading-objective">Objective</h3>
<p>The main objective of cross-browser testing is to verify that the website or web app looks, works, and behaves the same or very similarly across various browsers, such as Google Chrome, Mozilla Firefox, Apple Safari, Microsoft Edge, and others. It aims to eliminate visual discrepancies, functional issues, and inconsistencies in user experience.</p>
<p>The primary goal of compatibility testing is to ensure that the website or application is compatible with a wide array of user environments, such as browsers, operating systems (Windows, macOS, Android, iOS), devices (desktops, laptops, tablets, smartphones), and network conditions (internet speeds and connectivity types).</p>
<h3 id="heading-challenges">Challenges</h3>
<p>Challenges in cross-browser testing arise from variations in how browsers interpret HTML, CSS, and JavaScript code, as well as differences in supported features and standards compliance.</p>
<p>Challenges in compatibility testing include addressing issues related to device-specific features, operating system dependencies, and network-related performance problems in addition to cross-browser challenges.</p>
<p>In summary, cross-browser testing is a subset of compatibility testing. While cross-browser testing specifically focuses on ensuring consistent performance across different web browsers and versions, compatibility testing encompasses a wider range of factors, including browsers, operating systems, devices, and network conditions. This helps guarantee a seamless user experience across diverse user environments.</p>
<p>Both types of testing are crucial for delivering high-quality web applications that meet the needs of a broad user base.</p>
<h2 id="heading-importance-of-cross-browser-compatibility-testing">Importance of Cross-Browser Compatibility Testing</h2>
<p>Neglecting this crucial aspect of web development can result in missed opportunities and potential damage to your brand's reputation.</p>
<h3 id="heading-user-experience">User Experience</h3>
<p>Users access websites and web applications through a variety of browsers and devices. Ensuring compatibility across these platforms guarantees a consistent and seamless experience for all users.</p>
<p>Frustration resulting from a poorly rendered website on a specific browser can lead to a high bounce rate and loss of potential customers or visitors.</p>
<h3 id="heading-market-reach">Market Reach</h3>
<p>Different users prefer different web browsers. Ignoring compatibility testing means potentially alienating a significant portion of your audience.</p>
<p>By ensuring your website works well on popular browsers like Chrome, Firefox, Safari, and Edge, you maximize your market reach and accessibility.</p>
<h3 id="heading-maintaining-credibility">Maintaining Credibility</h3>
<p>A website that functions well across browsers reflects professionalism and attention to detail. On the contrary, a website with compatibility issues can harm your brand's credibility and reputation. Users might perceive your site as unreliable or poorly developed.</p>
<h3 id="heading-mobile-devices">Mobile Devices</h3>
<p>Mobile browsers come with their own set of quirks and challenges. Given the rise in mobile internet usage, ensuring compatibility with mobile browsers is crucial.</p>
<p>A website that adapts well to varying screen sizes and touch interfaces is essential for catering to the mobile audience.</p>
<h3 id="heading-seo-impact">SEO Impact</h3>
<p>Search engines like Google consider user experience as a ranking factor. If your website performs poorly on specific browsers, it might affect your search engine rankings. A lower search ranking can significantly reduce organic traffic to your site.</p>
<h3 id="heading-support-and-maintenance">Support and Maintenance</h3>
<p>A website that works smoothly across different browsers reduces the burden of ongoing support and maintenance. Fewer compatibility-related issues mean fewer updates and patches needed, saving time and resources in the long run.</p>
<h3 id="heading-accessibility-compliance">Accessibility Compliance</h3>
<p>Accessibility is not only a legal requirement in many regions but also a moral imperative. Ensuring compatibility with screen readers and other assistive technologies allow people with disabilities to access and use your website.</p>
<p>Failure to meet accessibility standards can lead to legal consequences and damage to your brand's reputation.</p>
<h3 id="heading-global-audience">Global Audience</h3>
<p>The internet connects people worldwide. International users will access your website using various browsers. Cross-browser compatibility ensures that language characters, fonts, and other regional aspects display correctly, enabling you to cater to a global audience effectively.</p>
<h3 id="heading-competitive-advantage">Competitive Advantage</h3>
<p>Websites that prioritize cross-browser compatibility gain a competitive edge. They can attract and retain users more effectively than those with compatibility issues.</p>
<p>A well-optimized site provides a better user experience, leading to higher user engagement and potentially higher conversion rates.</p>
<h2 id="heading-types-of-cross-browser-compatibility-testing">Types of Cross-Browser Compatibility Testing</h2>
<p>Here are some of the main types of cross-browser and compatibility testing:</p>
<h3 id="heading-functional-testing">Functional Testing</h3>
<p>This type of testing checks if all the interactive features and functionalities of a website work as expected across different browsers.</p>
<p>Examples are ensuring that forms can be submitted, buttons are clickable, navigation menus function correctly, and scripting interactions behave consistently.</p>
<h3 id="heading-visual-testing">Visual Testing</h3>
<p>This type of testing focuses on the visual appearance of a website or application across different browsers and devices.</p>
<p>Examples are verifying that fonts, colors, layouts, and images are displayed consistently, and that there are no visual glitches or misalignments.</p>
<h3 id="heading-performance-testing">Performance Testing</h3>
<p>This type of testing assesses how a website performs in terms of loading speed and responsiveness across various browsers and devices.</p>
<p>Examples are measuring page load times, checking the site's responsiveness on different screen sizes, and ensuring that resource-intensive features (videos or animations) do not cause performance issues.</p>
<h3 id="heading-cross-device-testing">Cross-Device Testing</h3>
<p>This type of testing ensures that a website functions properly on a range of devices, including desktops, laptops, tablets, and mobile phones.</p>
<p>Examples are testing touch interactions on mobile devices, verifying responsiveness on different screen resolutions, and confirming compatibility with various device orientations (landscape and portrait).</p>
<h3 id="heading-cross-platform-testing">Cross-Platform Testing</h3>
<p>This type of testing involves checking compatibility across different operating systems and browsers.</p>
<p>Example is to verify that the website functions consistently on both Windows and macOS computers, as well as Android and iOS devices.</p>
<h3 id="heading-browser-version-testing">Browser Version Testing</h3>
<p>This type of testing involves testing a website on different versions of a particular browser to ensure compatibility across various iterations.</p>
<p>An example is to test on older versions of popular browsers like Internet Explorer 11, or older versions of Firefox or Chrome, to support users who have not updated their browsers.</p>
<h3 id="heading-accessibility-testing">Accessibility Testing</h3>
<p>This type of testing helps ensure that a website is usable by people with disabilities and complies with accessibility standards such as WCAG (Web Content Accessibility Guidelines).</p>
<p>Examples are testing keyboard navigation, screen reader compatibility, and the use of ARIA (Accessible Rich Internet Applications) attributes to make the site more accessible to users with disabilities.</p>
<h3 id="heading-security-testing">Security Testing</h3>
<p>Security testing verifies that a website's security features and protocols work consistently across different browsers and platforms.</p>
<p>Examples are to ensure that SSL (Secure Sockets Layer) certificates are correctly implemented, that login forms are secure, and that security headers like Content Security Policy (CSP) are effective.</p>
<p>This comprehensive testing approach helps ensure a seamless and consistent user experience across diverse user environments.</p>
<h2 id="heading-roles-and-collaboration-in-cross-browser-compatibility-testing">Roles and Collaboration in Cross-Browser Compatibility Testing</h2>
<p>Cross-browser compatibility testing involves web developers, designers, and quality testers working together. Developers write clean code, designers ensure visual consistency, and testers find and document issues.</p>
<p>Collaboration is key. Developers and designers create flexible designs, and testers rely on their expertise. Communication is vital for resolving issues promptly and meeting compatibility standards.</p>
<p>External collaboration with users and clients is also crucial. User feedback helps identify real-world issues, and managing client expectations aligns with browser capabilities. Successful testing relies on technical know-how and a collaborative culture within the team and with external stakeholders.</p>
<h2 id="heading-best-practices-for-cross-browser-compatibility-testing">Best Practices for Cross-Browser Compatibility Testing</h2>
<p>By adhering to these best practices, web developers and testers can effectively tackle cross-browser compatibility challenges and deliver web experiences that are reliable and user-friendly across a wide range of browsers and devices.</p>
<ul>
<li><p>Identify Target Browsers: Determine which browsers are most commonly used by your target audience. Focus your testing efforts on these browsers to ensure the best user experience for the majority of your visitors.</p>
</li>
<li><p>Prioritize Popular Browsers: Give higher priority to testing on the most popular web browsers such as Google Chrome, Mozilla Firefox, Apple Safari, and Microsoft Edge. These browsers have larger user bases and are more likely to be used by your visitors.</p>
</li>
<li><p>Test on Mobile Devices: Don't forget to test on mobile browsers, including iOS Safari and Android Chrome, as mobile users make up a significant portion of internet traffic. Ensure that your website is responsive and mobile-friendly.</p>
</li>
<li><p>Use Browser Developer Tools: Familiarize yourself with the developer tools available in modern browsers. These tools allow you to inspect elements,<br>  debug JavaScript, and simulate different browser environments, making it easier to identify and fix issues.</p>
</li>
<li><p>Leverage Cross-Browser Testing Tools: Consider using cross-browser testing tools and services like BrowserStack, CrossBrowserTesting, or Sauce Labs.<br>  These platforms provide access to a wide range of browser and OS combinations, allowing you to test efficiently without setting up multiple physical environments.</p>
</li>
<li><p>Regularly Update Your Browser List: Keep your list of target browsers up to date. Browsers release new versions regularly, and older versions may become less relevant. Test on the latest browser versions to address potential issues before they become widespread.</p>
</li>
<li><p>Validate HTML and CSS: Use validation tools such as the <a target="_blank" href="https://validator.w3.org/">W3C Markup Validation Service</a> and <a target="_blank" href="https://www.cssportal.com/css-validator/">CSS Validator</a> to check your code for compliance with web standards. Valid code is more likely to render consistently across browsers.</p>
</li>
<li><p>Implement Graceful Degradation and Progressive Enhancement: Design your website with a "baseline" experience that works on all browsers and devices. Then, enhance the experience for modern browsers with additional features.<br>  This approach ensures that all users have a functional experience.</p>
</li>
<li><p>Perform Automated Testing: Consider setting up automated testing using tools like <a target="_blank" href="https://www.selenium.dev/">Selenium</a>, <a target="_blank" href="https://pptr.dev/">Puppeteer</a>, or <a target="_blank" href="https://testcafe.io/">TestCafe</a>. These tools allow you to create and run test scripts across various browsers automatically, saving time and ensuring consistency.</p>
</li>
<li><p>Regularly Review and Update: Stay informed about the latest developments in web standards, browser updates, and best practices. Periodically review and update your testing procedures to remain effective in ensuring cross-browser compatibility.</p>
</li>
<li><p>Test Accessibility: Ensure that your website complies with web accessibility standards, such as WCAG. Test with screen readers and other assistive technologies to make your site accessible to users with disabilities.</p>
</li>
<li><p>Document and Track Issues: Maintain a detailed log of cross-browser compatibility issues and their resolutions. This documentation will help your team address similar issues in the future and maintain a high level of consistency.</p>
</li>
</ul>
<h2 id="heading-tools-for-cross-browser-testing">Tools for Cross-Browser Testing</h2>
<p>You have access to various tools that can help you test your web projects efficiently. Let's look at a few popular ones now:</p>
<h3 id="heading-browserstack">BrowserStack</h3>
<p>This is a popular cloud-based cross-browser testing platform that allows you to test your website or web app on a wide range of browsers and operating systems.</p>
<p>It provides access to real browser instances for manual testing and supports automated testing with Selenium and Appium.</p>
<h3 id="heading-crossbrowsertesting">CrossBrowserTesting</h3>
<p>This is a cloud-based testing platform that offers a vast array of browsers and devices for cross-browser testing.</p>
<p>It provides live interactive testing as well as automated testing capabilities and integrates with various testing frameworks.</p>
<h3 id="heading-sauce-labs">Sauce Labs</h3>
<p>This is another cloud-based testing platform that offers a comprehensive range of browser and device combinations for testing web and mobile applications.</p>
<p>It supports both manual and automated testing and integrates with popular testing frameworks like Selenium and Appium.</p>
<h3 id="heading-lambdatest">LambdaTest</h3>
<p>This is a cloud-based cross-browser testing platform that provides access to a large selection of browsers and operating systems.</p>
<p>It offers live interactive testing and supports automated testing with popular testing frameworks.</p>
<h3 id="heading-browserling">Browserling</h3>
<p>This is a web-based tool that allows you to quickly test your website on a variety of browsers without the need for downloads or installations.</p>
<p>It offers real-time browser access and is suitable for quick checks and debugging.</p>
<h3 id="heading-browsershots">Browsershots</h3>
<p>This is an open-source tool that provides screenshots of your website or web app as it appears in different browsers and versions. While it doesn't offer live testing or interaction, it's useful for visual comparisons.</p>
<h3 id="heading-blisk">Blisk</h3>
<p>This is a browser specifically designed for web development and testing. It provides a side-by-side view of your website in multiple devices and browsers, making it easier to spot compatibility issues during development.</p>
<h3 id="heading-ghostlab">Ghostlab</h3>
<p>This is a paid tool for synchronized testing and debugging across multiple devices and browsers. It helps you inspect and debug issues in real time while maintaining synchronization between devices.</p>
<h3 id="heading-browser-devtools">Browser DevTools</h3>
<p>Most modern browsers, including Chrome, Firefox, Safari, and Edge, come with built-in developer tools. These tools are essential for inspecting, debugging, and testing websites directly in the browser environment. They offer features for emulating different browsers, devices, and network conditions.</p>
<p>These tools vary in terms of features, pricing, and ease of use, so it's essential to choose the one that best fits your specific cross-browser testing needs and budget.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Cross-browser compatibility testing is an essential aspect of web development. By following these best practices, you can ensure that your web applications provide a consistent and delightful user experience across different browsers.</p>
<p>A combination of cloud-based testing platforms and browser developer tools can provide comprehensive coverage for testing your web projects on different browsers and ensuring a consistent user experience.</p>
<p>Remember that the web development field is constantly changing, so continuous learning and adaptation are key to your success.</p>
<p>If you found this guide helpful and enjoyable, please give it a like. For more insightful tutorials, follow me on <a target="_blank" href="https://twitter.com/casweb_dev">X</a> for updates <strong>🙏</strong>.</p>
<p>Happy coding, and may your web apps thrive in every corner of the internet!</p>
<p>Kudos to ValueCoders for the cover image image.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Lifecycle Methods and Hooks – a Beginner's Guide ]]>
                </title>
                <description>
                    <![CDATA[ React is all about building user interfaces. And to do that effectively, React provides ways for components to manage their lifecycles. This means that components can perform specific tasks at different stages of their existence, from the moment they... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-lifecycle-methods-and-hooks-for-beginners/</link>
                <guid isPermaLink="false">66d45e08680e33282da25e59</guid>
                
                    <category>
                        <![CDATA[ hooks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ lifecycle methods ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Mon, 02 Oct 2023 17:22:49 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/10/lifecycle.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><a target="_blank" href="https://www.freecodecamp.org/news/react-beginner-handbook/#howmuchjavascriptyouneedtoknowtousereact">React</a> is all about building user interfaces. And to do that effectively, React provides ways for components to manage their lifecycles.</p>
<p>This means that components can perform specific tasks at different stages of their existence, from the moment they are created to the point they are removed from the user interface.</p>
<p>Lifecycle methods have been a fundamental part of React for many years. But with the introduction of hooks, React's approach to managing state and side effects in functional components has become more intuitive and flexible.</p>
<p>Just a quick note: although hooks generally replace class components, there are no plans to remove classes from React.</p>
<h3 id="heading-why-this-guide">Why This Guide?</h3>
<p>In this tutorial, you will learn about class component lifecycle methods such as <code>componentDidMount</code>, <code>componentDidUpdate</code>, <code>componentWillUnmount</code>, and <code>shouldComponentUpdate</code>.</p>
<p>You'll also explore React hooks like <code>useState</code>, <code>useEffect</code>, and <code>useContext</code>, and understand why they were introduced. This will make your React journey smoother and more enjoyable.</p>
<p>Whether you're just getting started with React or looking to deepen your understanding, this guide will equip you with the knowledge you need to build responsive and interactive web applications using React's powerful tools.</p>
<p>Let's dive in and uncover the magic of React lifecycle methods and hooks.</p>
<h2 id="heading-how-the-component-lifecycle-works">How the Component Lifecycle Works</h2>
<p>In React, components go through a lifecycle composed of distinct stages. Each of these stages offers specific methods that you can customize to run code at various moments during a component's existence.</p>
<p>These methods help you perform tasks such as initializing data, managing updates, and tidying up resources as needed.</p>
<h3 id="heading-class-component-lifecycle-methods">Class Component Lifecycle Methods</h3>
<p>Let's start by looking at the class component lifecycle methods. These were the primary way to manage component lifecycle before the introduction of hooks.</p>
<h4 id="heading-how-to-use-componentdidmount">How to use <code>componentDidMount</code>:</h4>
<p>This is called after a component has been inserted into the DOM. It's a great place to perform initial setup tasks, like fetching data from an API or setting up event listeners.</p>
<p>Code example:</p>
<pre><code class="lang-jsx">
<span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>() {
    <span class="hljs-built_in">super</span>();
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">data</span>: <span class="hljs-literal">null</span>,
    };
  }

  componentDidMount() {
    <span class="hljs-comment">// This is where you can perform initial setup.</span>

    <span class="hljs-comment">// In this example, we simulate fetching data from an API after the             component has mounted.</span>
    <span class="hljs-comment">// We use a setTimeout to mimic an asynchronous operation.</span>
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      <span class="hljs-keyword">const</span> fetchedData = <span class="hljs-string">'This data was fetched after mounting.'</span>;
      <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">data</span>: fetchedData });
    }, <span class="hljs-number">2000</span>); <span class="hljs-comment">// Simulate a 2-second delay</span>
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>componentDidMount Example<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        {this.state.data ? (
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Data: {this.state.data}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        ) : (
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Loading data...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        )}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p>In this example, we created a class component called <code>MyComponent</code>. In the constructor, the component's state is initialized with data set to null, and we use it to store the fetched data.</p>
<p>In the <code>componentDidMount</code> method, we simulate fetching data from an API using <code>setTimeout</code> to mimic an asynchronous operation. After 2 seconds (2000 milliseconds), the component's state updates with the fetched data.</p>
<p>In the render method, content is conditionally rendered based on the data state. If data is null, a <code>Loading data...</code> message is displayed. Otherwise, the fetched data is displayed.</p>
<p>When you use this component in your application, you'll notice that the Loading data... message is shown initially, and after 2 seconds, the fetched data is displayed. This demonstrates how <code>componentDidMount</code> is useful for performing tasks after a component has been added to the DOM.</p>
<h4 id="heading-how-to-use-componentdidupdateb">How to use <code>componentDidUpdate</code>B:</h4>
<p>This is called after a component has re-rendered due to changes in its state or props. It's a great place to handle side effects or perform additional actions based on those changes.</p>
<p>Code Example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>() {
    <span class="hljs-built_in">super</span>();
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">count</span>: <span class="hljs-number">0</span>,
    };
  }

  <span class="hljs-comment">// This method will be called when the "Increment" button is clicked</span>
  handleIncrement = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">count</span>: <span class="hljs-built_in">this</span>.state.count + <span class="hljs-number">1</span> });
  };

  <span class="hljs-comment">// componentDidUpdate is called after the component updates</span>
  componentDidUpdate(prevProps, prevState) {
    <span class="hljs-comment">// You can access the previous props and state here</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Component updated'</span>);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Previous state:'</span>, prevState);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Current state:'</span>, <span class="hljs-built_in">this</span>.state);
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Counter<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {this.state.count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleIncrement}</span>&gt;</span>Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Counter;
</code></pre>
<p>In this code example, we create a <code>Counter</code> class component with a constructor that initializes the <code>count</code> state to 0. The <code>handleIncrement</code> method updates the count state when the <em>Increment</em> button is clicked.</p>
<p>Inside the <code>componentDidUpdate</code> lifecycle method, we log a message (Component updated) to the console. We also log both the previous state (prevState) and the current state (this.state). This demonstrates how you can access both the previous and current values during an update. The render method displays the current count and a button to increment it.</p>
<p>Now, when you use this <code>Counter</code> component in your application, open the browser's console. Every time you click the <em>Increment</em> button, you'll see messages in the console indicating that the component has updated, along with the previous and current state values.</p>
<p>You can use <code>componentDidUpdate</code> for various purposes, such as making network requests when props or state change, updating the DOM based on state changes, or interacting with third-party libraries after an update. It provides a way to perform actions that should occur specifically after a component has re-rendered.</p>
<h4 id="heading-how-to-use-componentwillunmount">How to use <code>componentWillUnmount</code></h4>
<p>This is called just before a component is removed from the DOM. It's a crucial place to perform cleanup tasks, such as clearing timers, unsubscribing from events, or releasing resources to prevent [memory leaks](https://en.wikipedia.org/wiki/Memory_leak#:~:text=In computer science%2C a memory,longer needed is not released.).</p>
<p>Let's illustrate a simple React component that sets up a timer when it mounts, using <code>componentDidMount</code> method, and clears that timer when it unmounts using the <code>componentWillUnmount</code> method.</p>
<p>Code example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TimerComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>() {
    <span class="hljs-built_in">super</span>();
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">seconds</span>: <span class="hljs-number">0</span>,
    };
    <span class="hljs-built_in">this</span>.timer = <span class="hljs-literal">null</span>; <span class="hljs-comment">// Initialize the timer</span>
  }

  <span class="hljs-comment">// When the component mounts, start the timer</span>
  componentDidMount() {
    <span class="hljs-built_in">this</span>.timer = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">this</span>.setState({ <span class="hljs-attr">seconds</span>: <span class="hljs-built_in">this</span>.state.seconds + <span class="hljs-number">1</span> });
    }, <span class="hljs-number">1000</span>); <span class="hljs-comment">// Update every 1 second (1000 milliseconds)</span>
  }

  <span class="hljs-comment">// When the component unmounts, clear the timer to prevent memory leaks</span>
  componentWillUnmount() {
    <span class="hljs-built_in">clearInterval</span>(<span class="hljs-built_in">this</span>.timer);
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Timer Component<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Elapsed Time: {this.state.seconds} seconds<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> TimerComponent;
</code></pre>
<p>In this example, we created the <code>TimerComponent</code> class. Inside the constructor, the component's state is initialized with a seconds property, which we'll use to keep track of the elapsed time. The timer variable is also set to null.</p>
<p>In the <code>componentDidMount</code> lifecycle method, the timer is started by using <code>setInterval</code>. This timer increments the seconds state property every second.</p>
<p>In the <code>componentWillUnmount</code> lifecycle method, the timer is cleared using <code>clearInterval</code> to ensure that it doesn't continue running after the component has been removed from the DOM.</p>
<p>In the render method, the elapsed time is displayed based on the seconds state property.</p>
<p>When you use this <code>TimerComponent</code> in your application and render it, you'll notice that the timer starts when the component is mounted and stops when the component is unmounted. This is thanks to the cleanup performed in the <code>componentWillUnmount</code> method. This prevents resource leaks and ensures that<br>the timer is properly managed throughout the component's lifecycle.</p>
<h4 id="heading-how-to-use-shouldcomponentupdate">How to use <code>shouldComponentUpdate</code></h4>
<p>We use this lifecycle method to control whether a component should re-render when its state or props change. It is particularly useful for optimizing performance by preventing unnecessary renders.</p>
<p>Let's create a simple React class component and use the <code>shouldComponentUpdate</code> method to decide whether the component should re-render based on changes in its state.</p>
<p>Code Example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>() {
    <span class="hljs-built_in">super</span>();
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">count</span>: <span class="hljs-number">0</span>,
    };
  }

  shouldComponentUpdate(nextProps, nextState) {
    <span class="hljs-comment">// Allow the component to re-render only if the count is even</span>
    <span class="hljs-keyword">if</span> (nextState.count % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; <span class="hljs-comment">// Re-render</span>
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Don't re-render</span>
  }

  incrementCount = <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">this</span>.setState(<span class="hljs-function">(<span class="hljs-params">prevState</span>) =&gt;</span> ({ <span class="hljs-attr">count</span>: prevState.count + <span class="hljs-number">1</span> }));
  };

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Counter Example<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {this.state.count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.incrementCount}</span>&gt;</span>Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Counter;
</code></pre>
<p>In this example, we created the <code>Counter</code> class component that maintains a count state, which starts at 0. In the <code>shouldComponentUpdate</code> method, we check whether the next state's count is even. If it is, we allow the component to re-render. Otherwise, we prevent the re-render.</p>
<p>The <code>incrementCount</code> method is called when the <em>Increment</em> button is clicked. It updates the count state by incrementing it.</p>
<p>In the render method, the current count and a button to increment it is displayed.</p>
<p>If you click the <em>Increment</em> button and the count becomes an odd number, the component won't re-render. This behavior demonstrates how <code>shouldComponentUpdate</code> can be used to optimize rendering in situations where not all state changes should trigger a re-render.</p>
<h2 id="heading-introducing-react-hooks">Introducing React Hooks</h2>
<p>React introduced hooks in version 16.8. They granted functional components access to state and various React features without writing class components.</p>
<p>As a result, class components have become largely unnecessary. Hooks simplify component logic and make it more reusable.</p>
<h3 id="heading-why-use-hooks">Why use Hooks?</h3>
<p>Hooks were introduced to address several issues and make React code easier to understand and maintain:</p>
<ul>
<li><p>Complexity – class components can become complex when managing state and side effects.</p>
</li>
<li><p>Reusability – logic in class components isn't easily shareable between components.</p>
</li>
<li><p>Learning Curve – class components introduce a steeper learning curve for newcomers to React.</p>
</li>
</ul>
<h3 id="heading-commonly-used-react-hooks">Commonly used React Hooks</h3>
<h4 id="heading-the-usestate-hook">The <code>useState</code> hook</h4>
<p><code>useState</code> lets you add state to functional components. It returns an array with the current state value and a function to update it.</p>
<p>Code Example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setCount(count + 1)}&gt;Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>In this example, we used the <code>useState</code> hook to manage a counter's state. When the Increment button is clicked, <code>setCount</code> updates the count state, causing the component to re-render with the updated value.</p>
<h4 id="heading-the-useeffect-hook">The <code>useEffect</code> hook</h4>
<p><code>useEffect</code> is used for side effects in functional components, similar to <code>componentDidMount</code> and <code>componentDidUpdate</code>. It runs after rendering and can be controlled by specifying dependencies.</p>
<p>Code Example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Example</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Fetch data from an API</span>
    fetch(<span class="hljs-string">'https://api.example.com/data'</span>)
      .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> setData(data));
  }, []); <span class="hljs-comment">// Empty dependency array, runs only once</span>

  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{data ? data.message : 'Loading...'}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}
</code></pre>
<p>In this example, <code>useEffect</code> is used to fetch data from an API when the component mounts. The empty dependency array <code>[]</code> ensures that the effect runs only once.<br>When the data is fetched, <code>setData</code> updates the data state, causing a re-render with the fetched information.</p>
<h4 id="heading-the-usecontext-hook">The <code>useContext</code> hook</h4>
<p><code>useContext</code> allows functional components to access context values. It's a way to pass data down the component tree without explicitly passing props.</p>
<p>Code Example:</p>
<pre><code class="lang-jsx">
<span class="hljs-keyword">import</span> React, { useContext } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-comment">// Create a context</span>
<span class="hljs-keyword">const</span> MyContext = React.createContext();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> value = useContext(MyContext);

  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Context Value: {value}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}
</code></pre>
<p>In this example, we create a context called <code>MyContext</code>. The <code>useContext</code> hook allows <code>MyComponent</code> to access the value stored in this context. It's a powerful tool for managing global state in your application.</p>
<h3 id="heading-benefits-of-custom-hooks">Benefits of custom hooks</h3>
<p>Custom hooks are functions that use hooks internally and can be reused across multiple components. They help encapsulate and share complex logic.</p>
<p>Here's an example of a custom hook called <code>useLocalStorage</code> that simplifies storing and retrieving data in the browser's local storage:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useLocalStorage</span>(<span class="hljs-params">key, initialValue</span>) </span>{
  <span class="hljs-comment">// Retrieve the stored value from local storage</span>
  <span class="hljs-keyword">const</span> storedValue = <span class="hljs-built_in">localStorage</span>.getItem(key);

  <span class="hljs-comment">// Initialize the state with the stored value or the initial value</span>
  <span class="hljs-keyword">const</span> [value, setValue] = useState(storedValue || initialValue);

  <span class="hljs-comment">// Update the local storage whenever the state changes</span>
  <span class="hljs-keyword">const</span> setStoredValue = <span class="hljs-function">(<span class="hljs-params">newValue</span>) =&gt;</span> {
    setValue(newValue);
    <span class="hljs-built_in">localStorage</span>.setItem(key, newValue);
  };

  <span class="hljs-keyword">return</span> [value, setStoredValue];
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> useLocalStorage;
</code></pre>
<p>In this custom hook, we import <code>useState</code> from React because we'll use it to manage the state. The <code>useLocalStorage</code> function takes two parameters:</p>
<ul>
<li><p><strong>key</strong>: A string representing the key under which the data will be stored in local storage.</p>
</li>
<li><p><code>**initialValue**</code>: The initial value for the state.</p>
</li>
</ul>
<p>Inside the hook, we first attempted to retrieve the stored value from local storage using <code>localStorage.getItem(key)</code>. Then we initialized the state variable value using <code>useState</code>, using the <code>storedValue</code> if it exists or the <code>initialValue</code> if not.</p>
<p>Next, we defined a function <code>setStoredValue</code> that updates both the state and the local storage when called. It sets the new value in local storage using <code>localStorage.setItem(key, newValue)</code>.</p>
<p>Finally, we returned an array <code>[value, setStoredValue]</code> as the hook's return value, allowing components to access the stored value and update it as needed.</p>
<p>Here's an example of how you can use the <code>useLocalStorage</code> hook in a component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> useLocalStorage <span class="hljs-keyword">from</span> <span class="hljs-string">'./useLocalStorage'</span>; <span class="hljs-comment">// Import the custom hook</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Use the custom hook to manage a "username" stored in local storage</span>
  <span class="hljs-keyword">const</span> [username, setUsername] = useLocalStorage(<span class="hljs-string">'username'</span>, <span class="hljs-string">'Guest'</span>);

  <span class="hljs-keyword">const</span> handleInputChange = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
    setUsername(e.target.value);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello, {username}!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
        <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your username"</span>
        <span class="hljs-attr">value</span>=<span class="hljs-string">{username}</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleInputChange}</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>In this example, we import the <code>useLocalStorage</code> custom hook and use it to manage a username value in local storage. The component initializes the username state using the hook and updates it when the input field changes.</p>
<p>The value is stored and retrieved from local storage, allowing it to persist across page reloads.</p>
<p>Custom hooks are a powerful way to encapsulate and reuse complex logic in React applications, making your code more modular and maintainable.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>React provides developers with powerful tools to manage the lifecycles of their components. These lifecycles allow components to perform specific tasks at different stages of their existence, from creation to removal.</p>
<p>In this guide, we've explored React's class component lifecycle methods. These methods have been a fundamental part of React for many years and continue to be relevant in certain scenarios.</p>
<p>You've also been introduced to React Hooks. These have become the preferred way to manage state and side effects in React applications. They offer a more intuitive and flexible approach to building components.</p>
<p>While hooks have gained popularity and generally replace the need for class components, it's important to note that there are no plans to remove class components from React. Existing codebases and third-party libraries may still use class components, so understanding both class component lifecycles and hooks is<br>valuable for React developers.</p>
<p>In summary, React's lifecycle methods and hooks are crucial for building dynamic and efficient applications, and they offer developers a range of options to manage component behavior and state. As you continue to explore and work with React,<br>you'll find that having a solid understanding of both lifecycles and hooks will make you a more versatile and capable React developer.</p>
<p>If you found this guide helpful and enjoyable, please give it a like. For more insightful tutorials, follow me on <a target="_blank" href="https://twitter.com/casweb_dev">X</a> for updates 🙏.</p>
<p>Enjoy your coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use React Components – Props, Default Props, and PropTypes Explained ]]>
                </title>
                <description>
                    <![CDATA[ If you're curious about web development, you've probably heard the buzz about React. React is an open-source JavaScript library used for building user interfaces (UIs) in web applications. It has taken the web development world by storm. One of the k... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-react-components/</link>
                <guid isPermaLink="false">66d45e02182810487e0ce125</guid>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Tue, 05 Sep 2023 22:21:24 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/09/React-compo-2-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're curious about web development, you've probably heard the buzz about React.</p>
<p>React is an open-source JavaScript library used for building user interfaces (UIs) in web applications. It has taken the web development world by storm.</p>
<p>One of the key concepts in React is <strong>components.</strong> In this tutorial, we'll explore what React components are, how to pass and render data to them using props, and how to enhance their reliability using default props and propTypes.</p>
<h2 id="heading-what-are-react-components">What Are React Components?</h2>
<p>Imagine you're building a digital LEGO masterpiece. Each individual LEGO brick serves a purpose, but it's only when they're combined that something truly amazing takes shape.</p>
<p>In a similar way, React components are like those LEGO bricks. They are the individual building blocks that come together to create a full-fledged user interface. A component can be a button, a form, a header, or any other part of the webpage that you can think of.</p>
<p>Think of components as reusable templates that you can place throughout your web application to construct a complete user experience.</p>
<h2 id="heading-why-are-components-important-in-react">Why are Components Important in React?</h2>
<p>So, why are components such a big deal in React? There are a few reasons:</p>
<h3 id="heading-modularity-and-reusability">Modularity and Reusability</h3>
<p>By breaking down your UI into components, you create a modular structure. This means you can develop, test, and maintain each piece of your app separately. Plus, once you've created a component, you can reuse it throughout your app, saving you time and effort.</p>
<h3 id="heading-efficiency-in-development">Efficiency in Development</h3>
<p>Components encourage a more efficient development process. You can have different team members working on different components simultaneously without stepping on each other's toes. This speeds up the development process and encourages collaboration.</p>
<h3 id="heading-maintainability">Maintainability</h3>
<p>Imagine you need to update the styling of a button that appears in multiple places in your app. With components, you only need to update the styling in one place, the button's component, and it will automatically reflect wherever it's used.</p>
<h2 id="heading-types-of-react-components">Types of React Components</h2>
<p>There are two main types of components in React: functional components and class components.</p>
<h3 id="heading-how-to-use-functional-components">How to use functional components</h3>
<p>This is the simplest way to define components in React. They are basically JavaScript functions that take in <strong>props</strong> (input data) and return <strong>JSX</strong> (JavaScript Syntax Extension) elements.</p>
<p>Here's a code example to show you how they work:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>; <span class="hljs-comment">// Imports the React library.</span>

<span class="hljs-comment">// Define a functional component named "Button"</span>
<span class="hljs-keyword">const</span> Button = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>
      Click Me
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Button; <span class="hljs-comment">// Exports the Button component to make it accessible.</span>
</code></pre>
<p>In this example, we've defined a component called <code>Button</code> using a JavaScript function. This component returns a button element with the text "Click Me".</p>
<h3 id="heading-how-to-use-class-components">How to use class components</h3>
<p>These are JavaScript classes that extend the <strong>React.Component</strong> class.<br>They use ES6 classes and provide more advanced features, such as state management and lifecycle methods.</p>
<p>Here's a code example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>; <span class="hljs-comment">// Create class-based React components.</span>

<span class="hljs-comment">// Class component</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = { <span class="hljs-attr">count</span>: <span class="hljs-number">0</span> };
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {this.state.count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> this.setState({ count: this.state.count + 1 })}&gt;
          Increment
        <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Counter; <span class="hljs-comment">// Exports the Counter component to make it accessible.</span>
</code></pre>
<p>In this example, the <code>Counter</code> is a class component that maintains a <code>count</code> in its state and updates it when the button is clicked.</p>
<h3 id="heading-functional-components-vs-class-components-when-to-use-each">Functional components vs class components – when to use each</h3>
<p>Looking at the above two code snippets, you can differentiate them base on a few factors.</p>
<ul>
<li><p>Syntax: functional component are less verbose and easier to read due to their concise syntax. Class components are more verbose due to the class structure and the need to bind event handlers in the constructor.</p>
</li>
<li><p>State: until the introduction of <a target="_blank" href="https://www.freecodecamp.org/news/full-guide-to-react-hooks/">hooks</a>, functional components couldn't hold <a target="_blank" href="https://www.freecodecamp.org/news/react-state/#:~:text=In%20our%20React%20app%2C%20once,similarly%20to%20regular%20JavaScript%20functions.">State</a>. With hooks, you can now use the <strong>useState</strong> hook to manage state in functional components. Class component can hold state using the <code>this.state</code> property. State updates are done using <code>this.setState()</code>.</p>
</li>
<li><p>Lifecycle: functional components don't have lifecycle methods. Hooks like <strong>useEffect</strong> can be used to achieve similar effects. Class components support various <a target="_blank" href="https://www.freecodecamp.org/news/react-component-lifecycle-methods/">Lifecycle Methods</a> like <code>componentDidMount</code>, <code>componentDidUpdate</code>, <code>componentWillUnmount</code>, and so on.</p>
</li>
<li><p>Performance: functional components perform better as they don't need extra work required for creating classes. Class component can be a bit slower because of the extra work required for creating classes.</p>
</li>
<li><p>Recommended Use: functional components are preferred for most use cases in modern React development due to their simplicity and functional nature. Class components are still relevant for more complex scenarios that require state management and lifecycle methods (though hooks have made class components less necessary).</p>
</li>
</ul>
<p>React is continually evolving, and new patterns and features might emerge. Stay updated with the latest React documentation and best practices.</p>
<h2 id="heading-how-to-pass-data-to-components-using-props">How to Pass Data to Components using Props</h2>
<p>Props (properties) are like instructions you give to your components. They allow you to pass data from a parent component to a child component. This way, you can customize how components appear and behave.</p>
<p>In simple terms, props are like the parameters that you pass to a function. You can use props to customize the content and behavior of a component based on the values you provide when you use or render that component.</p>
<p>Here's a code example to show you how they work:</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// ParentComponent.js</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ChildComponent <span class="hljs-keyword">from</span> <span class="hljs-string">'./ChildComponent'</span>;

<span class="hljs-keyword">const</span> ParentComponent = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Cas"</span> /&gt;</span></span>;
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ParentComponent;
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-comment">// ChildComponent.js</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> ChildComponent = <span class="hljs-function">(<span class="hljs-params">props</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello, {props.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>;
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ChildComponent;
</code></pre>
<p>In this example, the <code>ParentComponent</code> passes the <code>name</code> prop to <code>ChildComponent</code>, which displays a personalized greeting, Hello, Cas!.</p>
<h3 id="heading-how-to-render-with-props">How to render with props</h3>
<p>Rendering with props allows you to create flexible and interactive user interfaces by passing and customizing data within your components. Once you have access to the data within the child component, you can use it to render dynamic content within the component's JSX. This means you can display different UI elements based on the values of the props.</p>
<p>In the <code>ParentComponent</code> used above, you can pass different values to the <code>name</code> prop to customize the displayed message.</p>
<p>Here's a code example:</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// ParentComponent.js</span>
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ChildComponent <span class="hljs-keyword">from</span> <span class="hljs-string">'./ChildComponent'</span>;

<span class="hljs-keyword">const</span> ParentComponent = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Cas"</span> /&gt;</span>;
<span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Nuel"</span> /&gt;</span>;
<span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Abbey"</span> /&gt;</span>;
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ParentComponent;
</code></pre>
<p>In this example, different values are provided for the <code>name</code> prop. The displayed messages will be:</p>
<ul>
<li><p><code>Hello, Cas!</code></p>
</li>
<li><p><code>Hello, Nuel!</code></p>
</li>
<li><p><code>Hello, Abbey!</code></p>
</li>
</ul>
<h3 id="heading-what-is-dynamic-rendering">What is dynamic rendering?</h3>
<p>Rendering with props becomes powerful when combined with JavaScript expressions. You can use props along with other variables, functions, or logic to render dynamic content. This flexibility enables you to create versatile and interactive user interfaces.</p>
<p>Here's a code example to show you how this works:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> Product = <span class="hljs-function">(<span class="hljs-params">props</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>{props.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Price: ${props.price}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      {props.isOnSale &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>On Sale!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Product;
</code></pre>
<p>In this example, the <code>Product</code> component takes multiple props to render details about a product, including its name, price, and whether it's on sale.<br>The expression <code>{props.isOnSale &amp;&amp; &lt;p&gt;On Sale!&lt;/p&gt;}</code> conditionally renders the "On Sale!" message if the <code>isOnSale</code> prop is true.</p>
<h3 id="heading-what-are-default-props">What are default props?</h3>
<p>In some cases, you might want to provide default values for props in case they aren't explicitly passed. This ensures your component doesn't break due to missing data.</p>
<p>Here's a code example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> ChildComponent = <span class="hljs-function">(<span class="hljs-params">props</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello, {props.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>;
};

ChildComponent.defaultProps = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"Guest"</span>,
};
</code></pre>
<p>In this example, if a value is not provided for the <code>name</code> property, it uses its default value of "Guest".</p>
<h3 id="heading-how-to-use-proptypes">How to use PropTypes</h3>
<p>To maintain the integrity of your application, you can specify the expected types for your props using propTypes.</p>
<p>Here's a code example:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">'prop-types'</span>;

ChildComponent.propTypes = {
  <span class="hljs-attr">name</span>: PropTypes.string.isRequired,
};
</code></pre>
<p>In this example, if the string is not provided for the property value, it will throw an error message.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Understanding the differences between functional and class components is crucial. Functional components, with their cleaner syntax and use of hooks, are favored for most use cases. Class components remain relevant for complex scenarios demanding state management and lifecycle methods.</p>
<p>Components thrive on the principle of passing data via props. Props are akin to instructions, allowing customization of component content and behavior. The capability to render with props enables dynamic and personalized user interfaces. When used in conjunction with JavaScript expressions, props enable versatile and interactive UIs.</p>
<p>Default props and PropTypes are additional tools that enhance component reliability. Default props provide fallback values to prevent breakage due to missing data, while PropTypes enforce data type validation, bolstering application integrity.</p>
<p>As React continues to evolve, staying updated with the latest documentation and best practices is essential. With the knowledge of components, props, default props, and PropTypes, you're well-equipped to embark on your React journey.</p>
<p>Interested in learning more about React? <a target="_blank" href="https://www.freecodecamp.org/news/react-beginner-handbook/#howmuchjavascriptyouneedtoknowtousereact">React for Beginners</a> covers nearly every aspect of React. I recommend it for a deeper insight.</p>
<p>If you found this guide helpful and enjoyable, please give it a like. For more insightful tutorials, follow me on <a target="_blank" href="https://twitter.com/casweb_dev">X</a> for updates 🙏.</p>
<p>Enjoy your coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Functions and Scope – a Beginner's Guide ]]>
                </title>
                <description>
                    <![CDATA[ Welcome to the exciting world of JavaScript Functions and Scope. Have you ever wondered how programs remember things and do tasks over and over again? Well, that's where functions and scope come into play. Whether you're a curious beginner or someone... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-functions-and-scope/</link>
                <guid isPermaLink="false">66d45e06230dff01669057bd</guid>
                
                    <category>
                        <![CDATA[ beginners guide ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Casmir Onyekani ]]>
                </dc:creator>
                <pubDate>Mon, 28 Aug 2023 14:55:54 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/08/learn-javascript-functions-and-scope-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Welcome to the exciting world of JavaScript Functions and Scope.</p>
<p>Have you ever wondered how programs remember things and do tasks over and over again? Well, that's where functions and scope come into play.</p>
<p>Whether you're a curious beginner or someone looking to strengthen your coding skills, get ready to unlock the secrets of functions and scope.</p>
<p>By the end of this tutorial, you'll be equipped with the knowledge to create more organized, efficient, and dynamic code.</p>
<p>If you're new to JavaScript, I suggest reading my guide to <a target="_blank" href="https://casblog.hashnode.dev/javascript-basics-a-beginners-guide-to-syntax-variables-operators-control-flow-and-loops">JavaScript Basics</a> before diving into this one.</p>
<p>Now, let's get into the fun stuff! 🚀</p>
<h2 id="heading-table-of-contents">Table of Contents:</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-introduction-to-javascript-functions-and-scope">Introduction to JavaScript Functions and Scope</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-declare-and-and-define-functions">How to Declare and and Define Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-function-parameters-and-arguments">Function Parameters and Arguments</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-return-statements-and-values-in-functions">Return Statements and Values in Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-are-anonymous-functions">What are Anonymous Functions?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-are-function-expressions">What are Function Expressions?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-arrow-functions-and-their-impact-on-this">Arrow Functions and Their Impact on "this"</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-does-function-and-variable-hoisting-work">How Does Function and Variable Hoisting Work?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-an-iife-immediately-invoked-function-expression">What is an IIFE (Immediately Invoked Function Expression)?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-default-parameters-in-a-javascript-function">How to Use Default Parameters in a JavaScript Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-rest-parameters-and-the-spread-operator-in-javascript-functions">How to Use Rest Parameters and the Spread Operator in JavaScript Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-destructure-function-parameters">How to Destructure Function Parameters</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-are-javascript-recursive-functions">What are JavaScript Recursive Functions?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-function-scope-and-closures-in-javascript">Function Scope and Closures in JavaScript</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-are-lexical-scope-and-closures">What are Lexical Scope and Closures?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-execution-context-and-the-call-stack">Execution Context and the Call Stack</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-debugging-and-troubleshooting-in-javascript">Debugging and Troubleshooting in JavaScript</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-introduction-to-javascript-functions-and-scope">Introduction to JavaScript Functions and Scope</h2>
<p>Functions let you group lines of code together and give them a name. They're like special tools that help you organize your code and perform specific actions whenever you need them.</p>
<p>Instead of writing the same code over and over, you can use functions to make your life easier. Consider functions as mini-programs that you can use and reuse to make your code more organized and efficient.</p>
<p>Scope is another fascinating concept that affects how your code works. It's like a set of rules that determine where your variables are allowed to hang out. Sometimes they're free to roam anywhere, and other times they're only allowed to stay within certain boundaries.</p>
<p>Don't stress if it sounds a little fancy. I'm here to explain everything clearly with examples that make sense.</p>
<h3 id="heading-how-to-declare-and-and-define-functions">How to Declare and and Define Functions</h3>
<p>Declaring a function is like announcing its name. Defining it is like giving it a purpose, this is where you put the code that the function will execute.</p>
<p>Here's an example of a simple function:</p>
<pre><code class="lang-js"><span class="hljs-comment">// This code is a function </span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
}

greet(<span class="hljs-string">"Cas"</span>); <span class="hljs-comment">// Output: Hello, Cas!</span>
</code></pre>
<p>In the above example, function called <code>greet</code> takes a <code>name</code> parameter and logs a greeting message using a <a target="_blank" href="https://www.freecodecamp.org/news/a-quick-introduction-to-tagged-template-literals-2a07fd54bc1d/">template literal</a>. Then, it calls the <code>greet</code> function with the argument "Cas" and outputs "Hello, Cas!".</p>
<h3 id="heading-function-parameters-and-arguments">Function Parameters and Arguments</h3>
<p>Imagine functions as machines that take inputs (parameters) and produce outputs.</p>
<p>Parameters are like placeholders for these inputs. Arguments are the actual values you give the function.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNumbers</span>(<span class="hljs-params">a, b</span>) </span>{  <span class="hljs-comment">//a, b are parameters</span>
  <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-keyword">const</span> result = addNumbers(<span class="hljs-number">5</span>, <span class="hljs-number">7</span>);  <span class="hljs-comment">//5,7 are arguments</span>
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: 12</span>
</code></pre>
<h3 id="heading-return-statements-and-values-in-functions">Return Statements and Values in Functions</h3>
<p>Assume you're sending your friend on a quest. They head out, complete the task, and return with a valuable item. In the world of functions, this "item" is what we call the return value. They're not just doing tasks – they deliver gifts! 🎉</p>
<p>It's the answer, the result, the prize that your function hands over once it's done with its mission.</p>
<p>Let's break it down with an example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiply</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">const</span> result = a * b;
  <span class="hljs-keyword">return</span> result;  <span class="hljs-comment">// The function gives back the 'result' as a gift</span>
}

<span class="hljs-keyword">const</span> product = multiply(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>);  <span class="hljs-comment">// The function is called, and the return value is captured</span>
<span class="hljs-built_in">console</span>.log(product);  <span class="hljs-comment">// Output: 15</span>
</code></pre>
<p>In the above example, the <code>multiply</code> function does its math, packages up the answer (the product of 3 and 5), and hands it over using the <code>return</code> statement.</p>
<p>Whether it's calculations, data processing, or generating valuable information,<br>return values allow your functions to contribute more to your overall code. So, get ready to embrace this concept as you continue your journey through JavaScript functions.</p>
<h3 id="heading-what-are-anonymous-functions">What are Anonymous Functions?</h3>
<p>Sometimes you don't need a named function. An anonymous function doesn't have a name – instead, it's defined directly where it's assigned. Anonymous functions are often used as callbacks or one-time-use functions.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> multiply = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-keyword">return</span> x * y;
}
</code></pre>
<p>This code defines an anonymous function assigned to the variable <code>multiply</code>, which takes two parameters <code>x</code> and <code>y</code> and returns their product when the function is called.</p>
<h3 id="heading-what-are-function-expressions">What are Function Expressions?</h3>
<p>These come to play when assigning functions to variables, pass functions as arguments to other functions, or return functions from other functions. It's an alternative to the more common function declaration.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> add = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
};

<span class="hljs-keyword">const</span> result = add(<span class="hljs-number">5</span>, <span class="hljs-number">3</span>);  <span class="hljs-comment">// Call the function</span>
<span class="hljs-built_in">console</span>.log(result);  <span class="hljs-comment">// Output: 8</span>
</code></pre>
<p>In this example, a function expression named <code>add</code> was defined and assigned to the variable <code>add</code>. The function takes two parameters <code>a</code> and <code>b</code>, and it returns the sum of these two numbers.</p>
<h3 id="heading-arrow-functions-and-their-impact-on-this">Arrow Functions and Their Impact on "this"</h3>
<p>This function behaves differently when it comes to the <code>this</code> keyword. Unlike regular functions, arrow functions don't create their own <code>this</code> context. Instead, they inherit the <code>this</code> value from their surrounding code.</p>
<p>Here's a code example showing that:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">regularFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);  <span class="hljs-comment">// Refers to the caller</span>
}

<span class="hljs-keyword">const</span> arrowFunction = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>);  <span class="hljs-comment">// Inherits from where it's defined</span>
};

<span class="hljs-keyword">const</span> obj = {
  <span class="hljs-attr">regular</span>: regularFunction,
  <span class="hljs-attr">arrow</span>: arrowFunction
};

obj.regular();  <span class="hljs-comment">// 'this' refers to 'obj'</span>
obj.arrow();    <span class="hljs-comment">// 'this' still refers to 'obj', despite being in an arrow function</span>
</code></pre>
<p>This code demonstrates the difference between regular functions and arrow functions regarding the usage of the <code>this</code> keyword. Arrow functions inherit the <code>this</code> context from where they are defined, while regular functions refer to the caller.</p>
<p>Another benefit of arrow functions is that they bring concise elegance to JavaScript. They're like a shorthand way of writing functions, perfect for simple tasks. When combined with default parameter values, they make your code even more streamlined.</p>
<p>Here's a code example of an arrow function with a default parameter:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> greet = <span class="hljs-function">(<span class="hljs-params">name = <span class="hljs-string">"friend"</span></span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
};

greet();        <span class="hljs-comment">// Output: Hello, friend!</span>
greet(<span class="hljs-string">"Cas"</span>); <span class="hljs-comment">// Output: Hello, Cas!</span>
</code></pre>
<p>In this example, the <code>name</code> parameter has a default value of "friend".</p>
<p>Arrow functions are especially handy when you want a quick way to define a function with default parameters.</p>
<h3 id="heading-how-does-function-and-variable-hoisting-work">How Does Function and Variable Hoisting Work?</h3>
<p>Hoisting is like setting up the stage before the play begins.</p>
<p>In JavaScript, function declarations are hoisted (raised) to the top of their containing scope. This means you can call a function before it's defined in your code.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-comment">// Function declaration (can be called anywhere)</span>
sayHello(); <span class="hljs-comment">// This code works</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello!"</span>);
}
</code></pre>
<p>The above code snippet works due to hoisting.</p>
<p>However, hoisting doesn't apply to function expressions:</p>
<pre><code class="lang-js"><span class="hljs-comment">// Function expreesion (called before defined)</span>
sayHi();  <span class="hljs-comment">// Error</span>

<span class="hljs-keyword">const</span> sayHi = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hi!"</span>);
};


<span class="hljs-comment">// Function expression (should be defined before calling)</span>
<span class="hljs-keyword">const</span> sayHello = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello!"</span>);
};

sayHello(); <span class="hljs-comment">// This works</span>
</code></pre>
<p>The <code>sayHi</code> function throws an error. Why? Because it's called before defined. This means that you must define a function expression before you attempt to call it.</p>
<p>Hoisting with the <code>let</code> and <code>const</code> Keywords has a slightly different behavior. They experience a <em>temporal dead zone</em>, just like the dancers waiting for their turn backstage.</p>
<p>The temporal dead zone in JavaScript refers to the period between the creation of a variable using the <code>let</code> or <code>const</code> keywords and the point where the variable is actually declared in the code.</p>
<p>During this period, if you try to access the variable, you'll get a reference error. This behavior is a result of how JavaScript's variable hoisting works with these block-scoped declarations.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(myName);  <span class="hljs-comment">// Throws an error - myName is not defined</span>
<span class="hljs-keyword">let</span> myName = <span class="hljs-string">"Cas"</span>;
</code></pre>
<p>In the above code, <code>myName</code> is hoisted, but trying to access it before the actual<br>declaration results in an error due to the temporal dead zone.</p>
<p>Note: While function hoisting can be helpful, it's a good practice to define your functions before using them to make your code more readable.</p>
<h3 id="heading-what-is-an-iife-immediately-invoked-function-expression">What is an IIFE (Immediately Invoked Function Expression)?</h3>
<p>Ever wanted to execute a function right after defining it? That's where <strong>IIFEs</strong> come into play. They're like the express lane of JavaScript.</p>
<p>All you need to do is to define the function, wrap it in parentheses, and then add another pair of parentheses to call it immediately. You can personalize your <strong>IIFE</strong> by adding a parameter.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js">(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
})(<span class="hljs-string">"Cas"</span>);
</code></pre>
<p>In this example, the <strong>IIFE</strong> takes the name "Cas" as a parameter and dances with it right away.</p>
<h3 id="heading-how-to-use-default-parameters-in-a-javascript-function">How to Use Default Parameters in a JavaScript Function</h3>
<p>In the world of JavaScript functions, flexibility is key. Sometimes, you want your function to handle missing or undefined values without causing errors. That's where default parameter values come to the rescue.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name = <span class="hljs-string">"Guest"</span></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
}

greet();          <span class="hljs-comment">// Output: Hello, Guest!</span>
greet(<span class="hljs-string">"Cas"</span>);   <span class="hljs-comment">// Output: Hello, Cas!</span>
</code></pre>
<p>In the <code>greet</code> function, the <code>name</code> parameter has a default value of "Guest". If you call the function without providing an argument for <code>name</code>, it will use the default value. If you provide an argument, it will override the default value</p>
<h3 id="heading-how-to-use-rest-parameters-and-the-spread-operator-in-javascript-functions">How to Use Rest Parameters and the Spread Operator in JavaScript Functions</h3>
<p>The <a target="_blank" href="https://www.freecodecamp.org/news/javascript-rest-vs-spread-operators/">Rest Parameter and the Spread Operator</a> are two related concepts in JavaScript that deal with handling and manipulating function arguments and arrays.</p>
<p>Imagine you're hosting a party, and you want to gather all the dishes your guests are bringing. The rest parameter is like a magical dish collector that grabs all the items your guests bring and puts them into an array for you to enjoy.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">partyPlanner</span>(<span class="hljs-params">mainDish, ...sideDishes</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Main dish: <span class="hljs-subst">${mainDish}</span>`</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Side dishes: <span class="hljs-subst">${sideDishes.join(<span class="hljs-string">', '</span>)}</span>`</span>);
}

partyPlanner( <span class="hljs-string">"Jollof rice"</span>, <span class="hljs-string">"Fufu"</span>, <span class="hljs-string">"Pizza"</span>, <span class="hljs-string">"Salad"</span>, <span class="hljs-string">"Kpomo"</span>, <span class="hljs-string">"Fries"</span>);
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Main dish: Jollof rice</span>
<span class="hljs-comment">// Side dishes: Fufu, Pizza, Salad, Kpomo, Fries</span>
</code></pre>
<p>In this example, the <code>...sideDishes</code> parameter collects all the extra values and packs them into an array, making it easy to work with varying numbers of inputs.</p>
<h3 id="heading-how-to-destructure-function-parameters">How to Destructure Function Parameters</h3>
<p>Let's say you receive a gift box with various items, and you want to unpack them and select the items you need immediately.</p>
<p>Destructuring helps you unpack and use the parts you need from complex data, like objects or arrays.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printPersonInfo</span>(<span class="hljs-params">{ firstName, lastName, age }</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`First Name: <span class="hljs-subst">${firstName}</span>`</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Last Name: <span class="hljs-subst">${lastName}</span>`</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Age: <span class="hljs-subst">${age}</span>`</span>);
}

<span class="hljs-keyword">const</span> person = {
  <span class="hljs-attr">firstName</span>: <span class="hljs-string">'Cas'</span>,
  <span class="hljs-attr">lastName</span>: <span class="hljs-string">'Nuel'</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>
};

printPersonInfo(person);
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// First Name: Cas</span>
<span class="hljs-comment">// Last Name: Nuel</span>
<span class="hljs-comment">// Age: 30</span>
</code></pre>
<p>In this example, the <code>printPersonInfo</code> function takes an object parameter. Instead of accessing the object properties using <code>person.firstName</code>, <code>person.lastName</code>, <code>person.Age</code>, we use destructuring within the function parameter list to directly extract the properties. This makes the code cleaner and more readable. When you call <code>printPersonInfo(person)</code>, the function will destructure the <code>person</code> object and print out its properties.</p>
<h3 id="heading-what-are-javascript-recursive-functions">What are JavaScript Recursive Functions?</h3>
<p>This is where a function calls itself to solve a problem by breaking it down into smaller, similar sub-problems.</p>
<p><a target="_blank" href="https://www.freecodecamp.org/news/recursion-in-javascript/">Recursion involves two main components</a>: a <strong>base condition</strong> that defines when the recursion should stop, and a <strong>recursive case</strong> where the function calls itself with modified parameters.</p>
<p>Here's a code example of a recursive function that calculates the factorial of a number:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">factorial</span>(<span class="hljs-params">n</span>) </span>{
  <span class="hljs-comment">// Base condition: factorial of 0 or 1 is 1</span>
  <span class="hljs-keyword">if</span> (n === <span class="hljs-number">0</span> || n === <span class="hljs-number">1</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  }

  <span class="hljs-comment">// Recursive case: call the function with a smaller sub-problem</span>
  <span class="hljs-keyword">return</span> n * factorial(n - <span class="hljs-number">1</span>);
}

<span class="hljs-keyword">const</span> num = <span class="hljs-number">5</span>;
<span class="hljs-keyword">const</span> result = factorial(num);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Factorial of <span class="hljs-subst">${num}</span> is <span class="hljs-subst">${result}</span>`</span>);
</code></pre>
<p>In this example, the <code>factorial</code> function calculates the factorial of a number <code>n</code>. The base condition checks if <code>n</code> is <strong>0</strong> or <strong>1</strong>. If it is, the function immediately returns <strong>1</strong>, as the factorial of <strong>0</strong> or <strong>1</strong> is <strong>1</strong>. The recursive case multiplies <code>n</code> with the result of calling the <code>factorial</code> function with <code>n - 1</code>.</p>
<p>This creates a chain of recursive calls, each reducing the problem by one and stops when it reaches the base condition. The calculated values are returned up the chain.</p>
<p>For example, when calling <code>factorial(5)</code>:</p>
<ul>
<li><p><code>factorial(5)</code> returns <code>5 * factorial(4)</code></p>
</li>
<li><p><code>factorial(4)</code> returns <code>4 * factorial(3)</code></p>
</li>
<li><p><code>factorial(3)</code> returns <code>3 * factorial(2)</code></p>
</li>
<li><p><code>factorial(2)</code> returns <code>2 * factorial(1)</code></p>
</li>
<li><p><code>factorial(1)</code> returns <code>1</code></p>
</li>
</ul>
<p>These values are then multiplied together, and the final result, which is <strong>120</strong>, is obtained.</p>
<p>Recursion is a powerful technique, but it's essential to have a well-defined base condition to avoid infinite loops. Each recursive call should move towards the base case, ensuring that the problem gets smaller with each iteration.</p>
<h3 id="heading-function-scope-and-closures-in-javascript">Function Scope and Closures in JavaScript</h3>
<p>With scope and closures you can organize your code, create private data, and build powerful functionalities.</p>
<p>It's like having little compartments in your coding toolbox that help you keep things tidy and efficient.</p>
<h4 id="heading-global-vs-local-scope">Global vs. Local Scope</h4>
<p>You can think of global scope as the entire neighborhood where all your houses (variables) live. Variables declared here are accessible from anywhere in your code.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> globalVariable = <span class="hljs-string">"I'm global!"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">globalScopeExample</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(globalVariable);  <span class="hljs-comment">// Accessing the global variable</span>
}

globalScopeExample();  <span class="hljs-comment">// Output: I'm global!</span>
</code></pre>
<p>This code defines a global variable <code>globalVariable</code> with a string value. Then, there's a function <code>globalScopeExample</code> that logs the value of <code>globalVariable</code>. The function is called, resulting in the output of the global variable's value.</p>
<p>On the other hand, local scope is like rooms within your houses. Variables declared inside functions or code blocks are local and can only be accessed within that function or block.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">localScopeExample</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> localVariable = <span class="hljs-string">"I'm local!"</span>;
  <span class="hljs-built_in">console</span>.log(localVariable);  <span class="hljs-comment">// Accessing the local variable</span>
}

localScopeExample();  <span class="hljs-comment">// Output: I'm local!</span>
<span class="hljs-comment">// console.log(localVariable);  // This would result in an error</span>
</code></pre>
<p>This code defines a function <code>localScopeExample</code> that creates a variable <code>localVariable</code> inside the function and then prints its value. When the function is called, it outputs the value of the <code>localVariable</code>. Attempting to access <code>localVariable</code> outside the function will result in an error</p>
<h3 id="heading-what-are-lexical-scope-and-closures">What are Lexical Scope and Closures?</h3>
<p>Lexical scope is a bit like those Russian nesting dolls. Each doll can access the dolls inside it, but not the other way around.</p>
<p>Similarly, in programming, it means an inner function can access variables from its outer function, but not vice versa.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">outer</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> outerVar = <span class="hljs-string">"I'm from outer function!"</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">inner</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(outerVar);  <span class="hljs-comment">// Accessing the outer variable</span>
  }

  inner();
}

outer();  <span class="hljs-comment">// Output: I'm from outer function!</span>
</code></pre>
<p>This code defines an outer function <code>outer</code> which contains a variable <code>outerVar</code>. Inside <code>outer</code>, there's an inner function <code>inner</code> that logs the value of <code>outerVar</code>. When <code>outer</code> is called, it also calls <code>inner</code>, resulting in the output "I'm from outer function!".</p>
<h4 id="heading-how-closures-work-and-why-theyre-important">How Closures Work and Why They're Important</h4>
<p>Closures are like time capsules that hold onto variables even after their functions have finished running. They're a combination of a function and the environment in which it was created.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rememberMe</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> secret = <span class="hljs-string">"I'm a secret!"</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(secret);  <span class="hljs-comment">// This inner function remembers the 'secret'</span>
  };
}

<span class="hljs-keyword">const</span> myClosure = rememberMe();
myClosure();  <span class="hljs-comment">// Output: I'm a secret!</span>
</code></pre>
<p>The code defines a function <code>rememberMe()</code> that creates and returns another function. This returned function, known as a closure, has access to the <code>secret</code> variable from its parent function's scope. When the <code>myClosure</code> function is invoked, it logs the value of the <code>secret</code> variable</p>
<p>Closures are great for creating private data or functions that only a specific part of your code can access.</p>
<p>Let's take another practical example of a closure:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> ++count;
  };
}

<span class="hljs-keyword">const</span> increment = counter();
<span class="hljs-built_in">console</span>.log(increment());  <span class="hljs-comment">// Output: 1</span>
<span class="hljs-built_in">console</span>.log(increment());  <span class="hljs-comment">// Output: 2</span>
</code></pre>
<p>The code creates a <code>counter</code> function that generates an incrementing counter each time it's called, demonstrating closure usage.</p>
<h3 id="heading-execution-context-and-the-call-stack">Execution Context and the Call Stack</h3>
<p>Every time a function is called, JavaScript creates an execution context. A sort of environment for that function to run in. It keeps track of variables, references, and where the function was called from.</p>
<p>Think of it as a backstage area where the function's code runs. All the variables, functions, and parameters are stored here.</p>
<p>Here's a code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">first</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from first!"</span>);
  second();  <span class="hljs-comment">// Calling another function</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">second</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from second!"</span>);
}

first();  <span class="hljs-comment">// Output: Hello from first! Hello from second!</span>
</code></pre>
<p>In the above example, function <code>first</code> calls function <code>second</code>, creating a new execution context for <code>second</code>.</p>
<p>The Call Stack is like a to-do list of functions waiting to be executed. When a function is called, it's added to the top of the stack. When it's done, it's removed.<br>This stack of contexts is what keeps track of where your code is.</p>
<h3 id="heading-debugging-and-troubleshooting-in-javascript">Debugging and Troubleshooting in JavaScript</h3>
<p>While sailing the seas of JavaScript, you're bound to encounter tricky issues<br>that can make your code behave unexpectedly.</p>
<p>But fret not, for I'm here to equip you with the tools, techniques, and strategies needed to steer your ship through these stormy waters.</p>
<p>Let's look at some common bugs and errors.</p>
<h4 id="heading-accidental-global-variables">Accidental Global Variables</h4>
<p>Look at this code example:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">oops</span>(<span class="hljs-params"></span>) </span>{
  myVariable = <span class="hljs-string">"I'm global!"</span>;  <span class="hljs-comment">// Oops, forgot 'var', 'let', or 'const'!</span>
}

oops();
<span class="hljs-built_in">console</span>.log(myVariable);  <span class="hljs-comment">// Output: I'm global!</span>
</code></pre>
<p>In this example, <code>myVariable</code> becomes global because you didn't use <code>var</code>, <code>let</code>, or <code>const</code> to declare it.</p>
<h4 id="heading-shadowing">Shadowing</h4>
<p>Look at this code example:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> x = <span class="hljs-number">10</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">shadowExample</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> x = <span class="hljs-number">5</span>;  <span class="hljs-comment">// This 'x' is different from the outer 'x'</span>
  <span class="hljs-built_in">console</span>.log(x);  <span class="hljs-comment">// Output: 5</span>
}

shadowExample();
<span class="hljs-built_in">console</span>.log(x);  <span class="hljs-comment">// Output: 10</span>
</code></pre>
<p>In this example, the inner x shadows the outer one, leading to different values within and outside the function.</p>
<h4 id="heading-debugging-tools-and-techniques">Debugging Tools and Techniques</h4>
<p>Modern browsers like Chrome come equipped with developer tools that let you set breakpoints, inspect variables, and step through your code line by line.</p>
<p><strong>Setting breakpoints</strong> involves using the browser's developer tools to pause your code at specific points (breakpoints) and examine the values of variables. This helps you pinpoint where things are going awry.</p>
<p><strong>Console logging</strong> involves inserting <code>console.log()</code> statements to print variable values or messages to the console. This can help you trace the flow of your code and identify unexpected behavior.</p>
<h4 id="heading-strategies-for-identifying-and-resolving-errors">Strategies for Identifying and Resolving Errors</h4>
<p>Dealing with scope issues requires a methodical approach. Here's your compass:</p>
<ul>
<li><p>Start Local: When debugging, start by checking the scope of variables.<br>  Are they in the right place? Are they shadowing other variables?</p>
</li>
<li><p>Step by Step: Use a debugger like browsers Dev tools, Visual Studio Code debugger, Node.js inspector to go through your code step by step. This helps you catch variables at different stages and spot any unexpected changes.</p>
</li>
<li><p>Isolate the Issue: If a function isn't behaving as expected, isolate it and test it separately. This can help you focus on the problematic part.</p>
</li>
<li><p>Review Your Code: Take a fresh look at your code, a second glance may reveal something you missed the first time.</p>
</li>
<li><p>Ask for Help: Don't be afraid to ask for help. Sometimes another set of eyes can spot what you've been missing.</p>
</li>
</ul>
<p>Navigating scope issues might feel like untangling a knot, but with practice, debugging becomes a skill that empowers you to conquer even the trickiest bugs.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, we've explored how functions can act as powerful tools and allow you to create organized and reusable code.</p>
<p>You also learned about scope, which is like a set of rules and dictates where variables can roam freely or stay within boundaries.</p>
<p>From basic function declarations to more advanced concepts like closures and arrow functions, you've also delved into how JavaScript functions work and the nuances of scope.</p>
<p>You've learned about execution context, the call stack, the quirks of hoisting, the use of default parameters, rest parameters, destructuring, and recursive function.</p>
<p>We also discussed debugging, a crucial skill, which equips you to navigate through errors, accidental global variables, and shadowing.</p>
<p>Armed with these insights and strategies, you're now well-prepared to craft more efficient and organized JavaScript code. You should be ready to conquer challenges and create dynamic applications.</p>
<p>To be more equipped on functions, I recommend you watch this <a target="_blank" href="https://www.youtube.com/watch?v=j1laALb8OVM">Mastering JavaScript Functions for Beginners</a> YouTube video.</p>
<p>If you found this guide helpful and enjoyable, please give it a like. For more insightful tutorials, follow me on <a target="_blank" href="https://twitter.com/casweb_dev">X</a> for updates 🙏.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
