<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Swarnali Roy</title>
    <description>The latest articles on Forem by Swarnali Roy (@swarnaliroy94).</description>
    <link>https://forem.com/swarnaliroy94</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F670691%2Fe2840baf-1877-447f-8ead-7c2bcfbbfa4b.jpg</url>
      <title>Forem: Swarnali Roy</title>
      <link>https://forem.com/swarnaliroy94</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/swarnaliroy94"/>
    <language>en</language>
    <item>
      <title>How to prepare for a Coding/Technical Interview</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Wed, 24 Dec 2025 20:07:14 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/how-to-prepare-for-a-codingtechnical-interview-2ihk</link>
      <guid>https://forem.com/swarnaliroy94/how-to-prepare-for-a-codingtechnical-interview-2ihk</guid>
      <description>&lt;h3&gt;
  
  
  What is a Coding Interview?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;coding interview&lt;/strong&gt;, also referred as &lt;strong&gt;technical interview&lt;/strong&gt;, is a specialized assessment used by technology companies to evaluate a candidate’s problem-solving skills, logic, and proficiency in software engineering. Unlike a standard behavioral interview, these sessions focus on one's ability to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;write clean code&lt;/li&gt;
&lt;li&gt;understand computer science fundamentals&lt;/li&gt;
&lt;li&gt;communicate your thought process in real-time&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Purpose
&lt;/h3&gt;

&lt;p&gt;The primary goal isn't just to see if a candidate can find the "right" answer, but to understand &lt;strong&gt;how one thinks&lt;/strong&gt;. &lt;br&gt;
Specifically, interviewers look for:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Problem-Solving Skills:&lt;/code&gt; Candidates are evaluated on their ability to deconstruct ambiguous prompts into logical, manageable steps. Interviewers look for a systematic approach — moving from a basic "brute-force" solution to a refined strategy — rather than an immediate, perfect answer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Technical Literacy:&lt;/code&gt; This measures a candidate’s grasp of computer science fundamentals, such as data structures and algorithms. They must be able to justify their technical choices and analyze efficiency using &lt;strong&gt;Big O notation&lt;/strong&gt;, specifically &lt;em&gt;O(n)&lt;/em&gt; or &lt;em&gt;O(\log n)&lt;/em&gt;, to show how the code scales with data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Communication:&lt;/code&gt; The candidate must "think out loud," clearly narrating their logic to ensure the interviewer can follow their thought process and verify that they can explain complex concepts to others.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Code Quality:&lt;/code&gt; Beyond solving the problem, the candidate must produce "&lt;strong&gt;production-ready&lt;/strong&gt;" code. This involves writing clean, readable syntax and proactively identifying edge cases — such as null inputs or empty sets — to ensure the solution is robust and reliable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Terms to Know
&lt;/h3&gt;

&lt;p&gt;If you're new to the technical scene, you'll often hear these two terms mentioned as the "gold standard" for professional code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Big O Notation:&lt;/code&gt; This is a mathematical way to describe how "fast" or "heavy" an algorithm is.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Time Complexity:&lt;/code&gt; How much longer the code takes to run as the input gets bigger (e.g., &lt;strong&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/strong&gt; means if the data doubles, the time doubles).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Space Complexity:&lt;/code&gt; How much extra memory the code needs to finish the job.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;DRY (Don’t Repeat Yourself):&lt;/code&gt; This is a core principle of clean coding. It means that if you are writing the same logic twice, you should probably move it into a single, reusable function. It makes your code easier to maintain and less prone to bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preparation Strategies
&lt;/h3&gt;

&lt;p&gt;o reduce performance anxiety and demonstrate a professional workflow, a successful candidate should focus on the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conceptual Planning &amp;amp; Clarification&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Identify Constraints:&lt;/code&gt; Ask about input sizes, data types, and memory limits before starting.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Align Expectations:&lt;/code&gt; Confirm the goal with the interviewer to avoid solving the wrong problem.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Manage Scope:&lt;/code&gt; Define what the solution will—and will not—handle initially.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Power of Pseudocode&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Logic First:&lt;/code&gt; Write out the step-by-step logic in plain English to validate the approach.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bridge the Gap:&lt;/code&gt; Use pseudocode to transition from a mental concept to actual syntax.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Visual Roadmap:&lt;/code&gt; Keep a high-level view of the solution to avoid getting lost in nested loops or complex logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Deconstruction of Complexity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Modularize:&lt;/code&gt; Split the main problem into smaller, independent functions or blocks.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Solve the "Easy" Parts First:&lt;/code&gt; Build momentum by tackling simple components before the core logic.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Iterative Debugging:&lt;/code&gt; Test small sections of the logic as they are built rather than waiting until the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Algorithmic Optimization&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Data Structure Selection&lt;/code&gt;: Choose between HashMaps, Trees, or Arrays based on the required search and insert speeds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Big O Analysis:&lt;/code&gt; Evaluate the time and space complexity (&lt;em&gt;O(n)&lt;/em&gt;) to ensure the solution scales.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Refactor for Efficiency:&lt;/code&gt; Remove redundant calculations and follow "&lt;strong&gt;DRY&lt;/strong&gt;" (Don't Repeat Yourself) principles to keep the codebase clean.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Do's/Don'ts during the interview
&lt;/h3&gt;

&lt;p&gt;Mastering the technical material is only half the battle; the other half is navigating the social and procedural dynamics of the session itself. The following guide highlights the critical behaviors that can either elevate a candidate’s standing or serve as immediate red flags.&lt;/p&gt;

&lt;p&gt;Most successful candidates follow a specific rhythm to stay on track:&lt;/p&gt;

&lt;p&gt;1.&lt;code&gt;Clarify:&lt;/code&gt; Ask targeted questions to uncover hidden constraints and confirm the problem requirements.&lt;br&gt;
2.&lt;code&gt;Plan:&lt;/code&gt; Narrate the strategy and logic out loud before typing a single line of code.&lt;br&gt;
3.&lt;code&gt;Implement:&lt;/code&gt; Write clean, structured code while explaining how each part solves the problem.&lt;br&gt;
4.&lt;code&gt;Test:&lt;/code&gt; Manually "dry run" the solution with a sample input to identify and fix bugs early.&lt;br&gt;
5.&lt;code&gt;Optimize:&lt;/code&gt; Analyze the solution's efficiency and discuss ways to improve its speed or memory usage.&lt;/p&gt;

&lt;p&gt;Candidates often fail when they fall into these common traps:&lt;/p&gt;

&lt;p&gt;1.&lt;code&gt;Rush:&lt;/code&gt; Diving straight into coding without a plan, which often leads to logic errors.&lt;br&gt;
2.&lt;code&gt;Silence:&lt;/code&gt; Coding in total silence, which prevents the interviewer from understanding the thought process.&lt;br&gt;
3.&lt;code&gt;Assume:&lt;/code&gt; Ignoring edge cases or failing to ask about data types and input sizes.&lt;br&gt;
4.&lt;code&gt;Resist:&lt;/code&gt; Becoming defensive or dismissive when the interviewer offers a hint or critique.&lt;br&gt;
5.&lt;code&gt;Neglect:&lt;/code&gt; Leaving the code messy or using vague variable names that make the logic hard to follow. &lt;/p&gt;

&lt;p&gt;While having a strong grasp of algorithms and data structures is vital, the ability to communicate logic and handle feedback is what truly sets one apart in a technical interview. By treating the interview as a collaborative exercise rather than a test of memorization, with consistent practice and a structured approach, a candidate can turn a high-pressure environment into an opportunity to showcase their best work.&lt;/p&gt;

</description>
      <category>coding</category>
      <category>interview</category>
      <category>beginners</category>
      <category>developers</category>
    </item>
    <item>
      <title>Choose your List Component wisely!</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Sat, 13 Sep 2025 18:31:05 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/choose-your-list-component-wisely-90g</link>
      <guid>https://forem.com/swarnaliroy94/choose-your-list-component-wisely-90g</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh" class="crayons-story__hidden-navigation-link"&gt;React Native: FlatList or SectionList, which one to use in your app?&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/swarnaliroy94" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F670691%2Fe2840baf-1877-447f-8ead-7c2bcfbbfa4b.jpg" alt="swarnaliroy94 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/swarnaliroy94" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Swarnali Roy
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Swarnali Roy
                
              
              &lt;div id="story-author-preview-content-2843652" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/swarnaliroy94" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F670691%2Fe2840baf-1877-447f-8ead-7c2bcfbbfa4b.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Swarnali Roy&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Sep 13 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh" id="article-link-2843652"&gt;
          React Native: FlatList or SectionList, which one to use in your app?
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/reactnative"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;reactnative&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/design"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;design&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/development"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;development&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;2&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            5 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>design</category>
      <category>development</category>
    </item>
    <item>
      <title>React Native: FlatList or SectionList, which one to use in your app?</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Sat, 13 Sep 2025 17:34:00 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh</link>
      <guid>https://forem.com/swarnaliroy94/react-native-flatlist-or-sectionlist-which-one-to-use-in-your-app-kkh</guid>
      <description>&lt;p&gt;In mobile apps, we spend most of the time browsing endless lists of content - a scrollable feed of posts, a stream of chat messages, a library of songs. Behind that smooth scrolling is smart engineering to keep the UI fast even with thousands of items.&lt;br&gt;
In &lt;strong&gt;React Native&lt;/strong&gt;, &lt;code&gt;FlatList&lt;/code&gt; and &lt;code&gt;SectionList&lt;/code&gt; are the two core, &lt;em&gt;virtualized&lt;/em&gt; list components designed to render large data efficiently.&lt;br&gt;
This post explains what these are, props that these provide, why you should use these, how these differ, and gives you battle-tested patterns to build fast, smooth lists.&lt;/p&gt;

&lt;p&gt;First of all, let's know what is a &lt;strong&gt;virtualized list&lt;/strong&gt;. A virtualized list (also called &lt;em&gt;list virtualization or windowing&lt;/em&gt;) is a front-end performance technique that renders only the items currently visible in the user’s viewport and reuses DOM nodes as the user scrolls. Instead of creating all list elements at once, most items stay in a virtual state, which significantly reduces memory use and improves rendering speed when working with large datasets.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is FlatList?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;FlatList&lt;/code&gt; renders a single, one-dimensional list of items (rows).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;FlatList&lt;/code&gt; component has &lt;strong&gt;two&lt;/strong&gt; required props you will need to pass to it as a bare minimum.&lt;/li&gt;
&lt;li&gt;The first prop is the &lt;code&gt;data&lt;/code&gt; that accepts a plain array. This array contains the list of items to display.&lt;/li&gt;
&lt;li&gt;The second prop is the &lt;code&gt;renderItem&lt;/code&gt; which takes an item from the &lt;code&gt;data&lt;/code&gt; and renders it into the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see a simple &lt;code&gt;FlatList&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;menuItemsToDisplay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;French Fries&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1A&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Buttered Rice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2B&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Baklava&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tiramisu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;4D&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lentil Soup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;5E&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Eggplant Salad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;6F&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;FlatList&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerContainer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MenuItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headerText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;Menu&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FlatList&lt;/span&gt;
        &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuItemsToDisplay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;keyExtractor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/FlatList&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An additional prop called the &lt;code&gt;keyExtractor&lt;/code&gt; is usually passed to the FlatList component. It instructs the list to use the id of each item as React keys.  &lt;/p&gt;

&lt;h4&gt;
  
  
  FlatList Props
&lt;/h4&gt;

&lt;p&gt;FlatList provides a wide range of props to customize list behavior and appearance.&lt;br&gt;
Among them, the following are most commonly used in everyday projects to structure headers, footers, separators, and scrolling direction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ItemSeparatorComponent&lt;/code&gt; – Renders a component between each item (e.g., a thin divider).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;horizontal&lt;/code&gt; – Makes the list scroll left-to-right instead of vertically.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ListHeaderComponent&lt;/code&gt; – A component displayed above all list items—great for search bars or filters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ListHeaderComponentStyle&lt;/code&gt; – Style object for the header wrapper (padding, margin, background).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ListFooterComponent&lt;/code&gt; – A component displayed below all list items—often used for loading spinners or “End of List” messages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ListFooterComponentStyle&lt;/code&gt; – Style object for the footer wrapper, useful for centering or spacing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To read about other props you can visit this link : &lt;a href="https://reactnative.dev/docs/flatlist" rel="noopener noreferrer"&gt;FlatList&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What is SectionList
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SectionList&lt;/code&gt; renders a list &lt;em&gt;grouped into sections&lt;/em&gt; — each section has a header and its own items. It’s still virtualized, but adds structure and features like &lt;strong&gt;sticky section headers&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;SectionList&lt;/code&gt; component has &lt;strong&gt;two&lt;/strong&gt; required props you will need to pass as a bare minimum. It is very similar to the &lt;code&gt;FlatList&lt;/code&gt; component.&lt;/li&gt;
&lt;li&gt;The first prop is the &lt;code&gt;sections&lt;/code&gt; that accept a plain array to display list of items.&lt;/li&gt;
&lt;li&gt;The second required prop is the &lt;code&gt;renderItem&lt;/code&gt; that takes an item from the &lt;code&gt;sections&lt;/code&gt; and renders it into the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see a simple &lt;code&gt;SectionList&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SectionList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StatusBar&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;SafeAreaView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SafeAreaProvider&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native-safe-area-context&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;menuItemsToDisplay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Main dishes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pizza&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Burger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Risotto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sides&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;French Fries&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Onion Rings&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fried Shrimps&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Drinks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Water&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Coke&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Beer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Desserts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cheese Cake&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ice Cream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MenuItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SafeAreaProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SafeAreaView&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;edges&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;top&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SectionList&lt;/span&gt;
        &lt;span class="nx"&gt;sections&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuItemsToDisplay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;keyExtractor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{({&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="nx"&gt;renderSectionHeader&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{({&lt;/span&gt;&lt;span class="na"&gt;section&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SafeAreaView&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SafeAreaProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code snippet, you can see that a prop called &lt;code&gt;renderSectionHeader&lt;/code&gt; is passed. This is a &lt;strong&gt;callback function&lt;/strong&gt; used to render each section’s header. These examples would be Main Dishes, Sides and so on. It renders &lt;code&gt;title&lt;/code&gt; from the menuItemsToDisplay array.  &lt;/p&gt;

&lt;h4&gt;
  
  
  SectionList Props
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;SectionList&lt;/code&gt; also offers many props to customize grouped lists.&lt;br&gt;
These are the most frequently used to handle section headers, separators, and grouping behavior:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stickySectionHeadersEnabled&lt;/code&gt; – Makes section headers stick to the top while scrolling through their section.&lt;br&gt;
&lt;code&gt;renderSectionHeader&lt;/code&gt; – Renders a component above each section’s items (e.g., an A–Z header or date label).&lt;br&gt;
&lt;code&gt;renderSectionFooter&lt;/code&gt; – Renders a component below each section’s items (e.g., a summary or action button).&lt;br&gt;
&lt;code&gt;SectionSeparatorComponent&lt;/code&gt; – Renders a component between entire sections, useful for extra spacing or dividers.&lt;/p&gt;

&lt;p&gt;To read about other props you can visit this link : &lt;a href="https://reactnative.dev/docs/sectionlist" rel="noopener noreferrer"&gt;SectionList&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Props in FlatList and SectionList
&lt;/h3&gt;

&lt;p&gt;Both &lt;code&gt;FlatList&lt;/code&gt; and &lt;code&gt;SectionList&lt;/code&gt; are built on top of React Native’s &lt;code&gt;VirtualizedList&lt;/code&gt;, so they share a powerful set of props to control rendering, performance and user interaction. Some common props that both of these use are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;initialNumToRender&lt;/code&gt; – Number of items to render initially (default is 10). Increase for faster first scroll, decrease for lighter startup load.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;windowSize&lt;/code&gt; – Number of screenfuls worth of items kept in memory (default is 21). Adjust to balance scroll smoothness vs. memory usage.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;maxToRenderPerBatch&lt;/code&gt; – Maximum number of items to render in each batch while scrolling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;removeClippedSubviews&lt;/code&gt; – Set to true (especially on Android) to unmount rows that scrolled out of view, saving memory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getItemLayout&lt;/code&gt; – Supplies fixed item height/width info so the list can skip measuring, enabling instant scrollToIndex and improving scroll performance.
&lt;code&gt;refreshing&lt;/code&gt; – Boolean to show a pull-to-refresh indicator.
&lt;code&gt;onRefresh&lt;/code&gt; – Callback triggered on pull-to-refresh.
&lt;code&gt;onEndReached&lt;/code&gt; – Fired when the list is close to the bottom, perfect for infinite scroll / pagination.
&lt;code&gt;onEndReachedThreshold&lt;/code&gt; – Distance from the bottom (in viewport heights) to trigger onEndReached (e.g., 0.5 = halfway).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both &lt;code&gt;FlatList&lt;/code&gt; and &lt;code&gt;SectionList&lt;/code&gt; renders items lazily, which means it only renders items you visually see on the screen, and once you start scrolling up or down, those items off the screen are removed, and the new items are rendered. This form of lazy rendering is very performant and effective in rendering large lists with sectional support in mobile apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to choose which?
&lt;/h3&gt;

&lt;p&gt;👉 Choose &lt;code&gt;FlatList&lt;/code&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your data is naturally a single, ungrouped stream (feed, chat, search results).&lt;/li&gt;
&lt;li&gt;You don’t need sticky headers or per-group UI.&lt;/li&gt;
&lt;li&gt;You want the lightest abstraction and maximum throughput.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Choose &lt;code&gt;SectionList&lt;/code&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need grouping (A–Z, by date, by category) and section headers.&lt;/li&gt;
&lt;li&gt;You want sticky headers without custom plumbing.&lt;/li&gt;
&lt;li&gt;You need visuals or logic between sections (SectionSeparatorComponent).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both components share powerful common props for performance tuning, refreshing, and pagination. By understanding their differences and capabilities, you can confidently choose the right list type, deliver smooth scrolling on any device, and keep your app fast—even with thousands of items.&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>design</category>
      <category>development</category>
    </item>
    <item>
      <title>Combining useful JS basics</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Mon, 01 Sep 2025 08:36:41 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/combining-useful-js-basics-29pb</link>
      <guid>https://forem.com/swarnaliroy94/combining-useful-js-basics-29pb</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko" class="crayons-story__hidden-navigation-link"&gt;Understanding map(), filter(), Set, and the Spread Operator in JavaScript&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/swarnaliroy94" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F670691%2Fe2840baf-1877-447f-8ead-7c2bcfbbfa4b.jpg" alt="swarnaliroy94 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/swarnaliroy94" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Swarnali Roy
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Swarnali Roy
                
              
              &lt;div id="story-author-preview-content-2812068" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/swarnaliroy94" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F670691%2Fe2840baf-1877-447f-8ead-7c2bcfbbfa4b.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Swarnali Roy&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Sep 1 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko" id="article-link-2812068"&gt;
          Understanding map(), filter(), Set, and the Spread Operator in JavaScript
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/beginners"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;beginners&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;5&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              2&lt;span class="hidden s:inline"&gt; comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding map(), filter(), Set, and the Spread Operator in JavaScript</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Mon, 01 Sep 2025 07:35:07 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko</link>
      <guid>https://forem.com/swarnaliroy94/understanding-map-filter-set-and-the-spread-operator-in-javascript-49ko</guid>
      <description>&lt;p&gt;When working with &lt;strong&gt;JavaScript (ES6+)&lt;/strong&gt; (especially in frameworks like &lt;strong&gt;React&lt;/strong&gt;), we often use four powerful features: &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt;, and the &lt;strong&gt;spread operator (...)&lt;/strong&gt;. &lt;br&gt;
Let's break these features with some simple examples and finally a practical example of combining all of these together. &lt;/p&gt;
&lt;h3&gt;
  
  
  1. The &lt;code&gt;map()&lt;/code&gt; Method
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;.map()&lt;/code&gt; method is used to transform each element of an array.&lt;/li&gt;
&lt;li&gt;It returns a new array of the same length.&lt;/li&gt;
&lt;li&gt;.map() does not change the original array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Syntax:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// return the new value for this element&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Explanation of parameters:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;element&lt;/code&gt; → the current item in the array&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index&lt;/code&gt; (optional) → the position of the element (0, 1, 2, …)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;array&lt;/code&gt; (optional) → the whole array being mapped&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6, 8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The condition is given inside the function &lt;code&gt;(num =&amp;gt; num * 2)&lt;/code&gt;.&lt;br&gt;
So &lt;code&gt;.map()&lt;/code&gt; will go through each element in the array &lt;code&gt;numbers&lt;/code&gt; and multiply it by 2. Then it will create a new array with the result value.&lt;/p&gt;

&lt;p&gt;👉 In React, map() is often used to render lists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mango&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The &lt;code&gt;filter()&lt;/code&gt; Method
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;.filter()&lt;/code&gt; method is used to keep only the elements that match a condition.&lt;/li&gt;
&lt;li&gt;It also returns a new array, without modifying the original.&lt;/li&gt;
&lt;li&gt;It does not execute the function for empty elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Syntax:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// return true to keep the element&lt;/span&gt;
  &lt;span class="c1"&gt;// return false to remove it&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation of parameters:
&lt;/h4&gt;

&lt;p&gt;Same as &lt;code&gt;.map()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evenNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The condition given inside the function &lt;code&gt;(num =&amp;gt; num % 2 === 0)&lt;/code&gt; simply means that only even numbers should be returned.&lt;br&gt;
So &lt;code&gt;.filter()&lt;/code&gt; will go through each element in the array &lt;code&gt;numbers&lt;/code&gt; and return a new array keeping only the even number elements.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. The Set Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A Set stores unique values only (no duplicates).&lt;/li&gt;
&lt;li&gt;It’s useful when you want to remove repeated items.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;categories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Healthy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dessert&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uniqueCategories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uniqueCategories&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Set { 'Fast Food', 'Healthy', 'Dessert' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need it back as an array:&lt;br&gt;
&lt;code&gt;const uniqueArray = [...uniqueCategories];&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  4. The Spread Operator (...)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The spread operator unpacks values from arrays, objects, or iterables.&lt;/li&gt;
&lt;li&gt;It’s useful for copying, merging, or expanding data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example with Arrays&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;combined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;combined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example with Objects&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'Alice', age: 26 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The spread operator&lt;/strong&gt; copies all the properties from the user object into the new object. &lt;/li&gt;
&lt;li&gt;After spreading, we also specify age: 26, which overrides the old age value (25) with the new one (26). &lt;/li&gt;
&lt;li&gt;As a result, the updated object contains the same name as before ("Alice") but with the updated age. &lt;/li&gt;
&lt;li&gt;Importantly, the original user object remains unchanged — we just created a new object with modified data.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Combining It All Together
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;availableCategories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;All&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foods&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;food&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;food&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;//Output : ['All', 'Fast Food', 'Healthy', 'Dessert']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-step explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Assume this is an array of food objects, e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pizza&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Salad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Healthy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Burger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;filter(food =&amp;gt; food.isAvailable)&lt;/code&gt; → keep only foods that are available.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pizza&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Burger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fast Food&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map(food =&amp;gt; food.category)&lt;/code&gt; → get their categories: &lt;code&gt;['Fast Food', 'Fast Food']&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Set(...)&lt;/code&gt; → remove duplicates : &lt;code&gt;Set { 'Fast Food' }&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;... spread operator&lt;/code&gt; → convert back into an array: &lt;code&gt;['Fast Food']&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add 'All' at the beginning for a universal option: &lt;code&gt;['All', 'Fast Food']&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these features make code cleaner, shorter, and easier to understand. Whether you’re building simple scripts or working in frameworks like React, mastering these concepts will give you a strong foundation for handling data in JavaScript. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Nested ScrollViews can be Tricky in React Native! How to solve?</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Thu, 22 Aug 2024 18:40:26 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/nested-scrollviews-can-be-tricky-in-react-native-how-to-solve-4f5a</link>
      <guid>https://forem.com/swarnaliroy94/nested-scrollviews-can-be-tricky-in-react-native-how-to-solve-4f5a</guid>
      <description>&lt;p&gt;Working with multiple components that use &lt;em&gt;Scrollview&lt;/em&gt; or &lt;em&gt;Flatlist&lt;/em&gt; in a single screen is a common practice in React Native. Often the nested scrolling behaviour doesn't work properly and can be tricky to use efficiently. I have struggled with this several times so thought to share the easy solution I found for this.&lt;/p&gt;

&lt;p&gt;Let's talk a bit about &lt;strong&gt;Scrollview&lt;/strong&gt; and &lt;strong&gt;Flatlist&lt;/strong&gt; at first!&lt;/p&gt;

&lt;h3&gt;
  
  
  ScrollView or Flatlist, which one to use?
&lt;/h3&gt;

&lt;p&gt;Typically, we use &lt;strong&gt;ScrollView&lt;/strong&gt; to make a screen scrollable in React Native. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This component allows the content to scroll vertically or horizontally when it exceeds the screen size. If the content inside the &lt;code&gt;ScrollView&lt;/code&gt; doesn't exceed the screen size, no scrolling may occur. &lt;/li&gt;
&lt;li&gt;Needs to have a bounded height in order to work properly, all parents elements also need to get a bounded height. Failing to do so might occur errors and lead to not achieving the desired scrolling behaviour. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ScrollView&lt;/code&gt; renders all its react child components at once. &lt;/li&gt;
&lt;li&gt;For displaying a very long list of contents, it may cause slow rendering and increased memory usage. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To achieve a more efficient rendering when dealing with long lists of data, using &lt;strong&gt;Flatlist&lt;/strong&gt; is definitely a better choice!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FlatList&lt;/code&gt; renders only the items that are currently visible on the screen and a few items before and after for smooth scrolling.&lt;/li&gt;
&lt;li&gt;Uses lazy loading and removes items that scroll way off screen to save memory and processing time.&lt;/li&gt;
&lt;li&gt;It offers various customization options such as custom item separators, headers, footers, and more, allowing user to create a tailored list experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am not going into much details about the usage of these two here as  you can get well documented examples in the official &lt;strong&gt;React Native Documentation&lt;/strong&gt; [&lt;a href="https://reactnative.dev/" rel="noopener noreferrer"&gt;https://reactnative.dev/&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;If your screen has only one component that needs to be scrollable, it's easier to choose depending on your project requirements. But the problem occurs, when you have to use multiple components in a single screen and more than one component needs to be scrollable! &lt;/p&gt;

&lt;h5&gt;
  
  
  It is recommended to &lt;em&gt;avoid&lt;/em&gt; nesting multiple &lt;code&gt;ScrollViews&lt;/code&gt; in a single screen.
&lt;/h5&gt;

&lt;h3&gt;
  
  
  Reasons to avoid Nested ScrollViews
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;When user use one &lt;code&gt;Scrollview&lt;/code&gt; inside another, the parent and child ScrollViews can compete for touch events, thus leads to unpredictable scrolling behaviour. &lt;/li&gt;
&lt;li&gt;The inner &lt;code&gt;ScrollView&lt;/code&gt; might not scroll properly or might interfere with the outer ScrollView's scrolling.&lt;/li&gt;
&lt;li&gt;The scrolling behaviour may vary across different devices and platforms (iOS vs Android).&lt;/li&gt;
&lt;li&gt;Performance can be degraded if the data or list is huge and the layout is complex.&lt;/li&gt;
&lt;li&gt;UI design might break, such as the height might not be adjusted as desired. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How to Solve This!
&lt;/h3&gt;

&lt;p&gt;Using a &lt;strong&gt;FlatList&lt;/strong&gt; instead of a &lt;strong&gt;ScrollView&lt;/strong&gt; for the inner scrollable component is a great way to solve this issue, especially when dealing with long lists of data. &lt;code&gt;FlatList&lt;/code&gt; is optimized for performance and provides features like pull-to-refresh and infinite scrolling. &lt;/p&gt;

&lt;p&gt;In my app, I needed the whole screen to be scrollable, so I used &lt;code&gt;ScrollView&lt;/code&gt; as the parent component. Inside that, I had to render a playlist which was a long list so I used a &lt;code&gt;Flatlist&lt;/code&gt; to render that in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Sample data for the FlatList&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Item &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ScrollView&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mainContainer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Main&lt;/span&gt; &lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Some content here */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contentSection&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;part&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="nx"&gt;scrollable&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Separately scrollable FlatList */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;separateScrollContainer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FlatList&lt;/span&gt;
          &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nx"&gt;keyExtractor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nx"&gt;nestedScrollEnabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contentSection&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;More&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="nx"&gt;scrollable&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ScrollView&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;mainContainer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="na"&gt;separateScrollContainer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Set a fixed height for the separate scrollable area&lt;/span&gt;
    &lt;span class="na"&gt;borderWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;borderColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gray&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first the &lt;em&gt;Flatlist&lt;/em&gt; scroll was not working properly and the height for the inner component was not adjusted, even though I used &lt;code&gt;flex: 1&lt;/code&gt; for the &lt;em&gt;main container&lt;/em&gt; . &lt;br&gt;
To make sure it works perfectly the main things that I had to do was: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;nestedScrollEnabled={true}&lt;/code&gt; on the FlatList&lt;/li&gt;
&lt;li&gt;Keeping the &lt;code&gt;FlatList&lt;/code&gt; inside a &lt;code&gt;View&lt;/code&gt; with a fixed height, as shown in the &lt;code&gt;separateScrollContainer&lt;/code&gt; style.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this way, the &lt;code&gt;FlatList&lt;/code&gt; automatically handles its own scrolling within the confined space.&lt;/p&gt;

&lt;h4&gt;
  
  
  Some Additional Tips
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FlatList&lt;/code&gt; props like &lt;code&gt;initialNumToRender&lt;/code&gt;, &lt;code&gt;maxToRenderPerBatch&lt;/code&gt;, and &lt;code&gt;windowSize&lt;/code&gt; can be used to optimize performance, especially if you're rendering a larger list.&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;FlatList&lt;/code&gt; content is shorter than its container, unwanted bouncing effects might occur. To prevent this situation, &lt;code&gt;bounces={false}&lt;/code&gt; can be added to the &lt;code&gt;FlatList&lt;/code&gt; props.&lt;/li&gt;
&lt;li&gt;If user wants the &lt;code&gt;FlatList&lt;/code&gt; to take up all remaining space, using &lt;code&gt;flexbox&lt;/code&gt; instead of a &lt;code&gt;fixed height&lt;/code&gt; for the wrapper &lt;code&gt;View&lt;/code&gt; of the &lt;code&gt;FlatList&lt;/code&gt; Component might be a better choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this helps solving nested ScrollView issues for people who are struggling building up some great quality React Native applications! &lt;/p&gt;

&lt;p&gt;Happy Coding! &lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>mobile</category>
      <category>development</category>
      <category>beginners</category>
    </item>
    <item>
      <title>CSS Variables with Global &amp; Local Scope</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Tue, 14 Jun 2022 19:23:37 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/css-variables-with-global-local-scope-4gbf</link>
      <guid>https://forem.com/swarnaliroy94/css-variables-with-global-local-scope-4gbf</guid>
      <description>&lt;p&gt;Hello everyone! &lt;br&gt;
It's been a long long time since I've posted anything. I am working as a Front-end Developer and suddenly, I realized that I have forgotten many &lt;strong&gt;CSS&lt;/strong&gt; basics. So, I started learning it once again.&lt;/p&gt;

&lt;p&gt;This blog is about &lt;strong&gt;CSS Variables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here I learned the usage of &lt;em&gt;CSS variables&lt;/em&gt; and how you can use them in your project. &lt;/p&gt;

&lt;p&gt;First of all, a CSS variable name must start with two dashes &lt;code&gt;--&lt;/code&gt; and it is &lt;em&gt;case sensitive&lt;/em&gt;. &lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--blue: #1e90ff;
--red: rgb(255,0,0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Global &amp;amp; Local Scope
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Declaring Global CSS Variable
&lt;/h3&gt;

&lt;p&gt;CSS Variables can have a &lt;strong&gt;global scope&lt;/strong&gt; and a &lt;strong&gt;local scope&lt;/strong&gt;. When you declare a CSS variable in a &lt;em&gt;global scope&lt;/em&gt;, it can be accessed throughout the entire document. &lt;/p&gt;

&lt;p&gt;To create a variable with global scope, we need to declare it inside the &lt;code&gt;:root&lt;/code&gt; selector. The &lt;code&gt;:root&lt;/code&gt; selector matches the document's root element. We declare a global CSS variable as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e90ff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;white&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;ffffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using CSS Variable in Local Scope
&lt;/h3&gt;

&lt;p&gt;To create a variable with local scope, we need to declare it inside the selector that is going to use it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;var()&lt;/code&gt; function is used to insert the value of a CSS variable. The syntax of the &lt;code&gt;var()&lt;/code&gt; function is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the above declared &lt;em&gt;global variables&lt;/em&gt; inside another selector we need to write it in the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;background&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;white&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;background&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;white&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;px&lt;/span&gt; &lt;span class="nx"&gt;solid&lt;/span&gt; &lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usefulness of CSS Variables
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;CSS variables have access to the DOM, so we can create it with both global and local scope.&lt;/li&gt;
&lt;li&gt;It can be changed with JavaScript.&lt;/li&gt;
&lt;li&gt;It can be changed based on media queries.&lt;/li&gt;
&lt;li&gt;The best way to use CSS variables is to change the color properties of your design. You can declare some variables and use them over and over again instead of copy-pasting the same colors.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I used CSS Variables in a FreeCodeCamp Learning project to change the colors of my design and it came out like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8k5mqutwgcswho5jwcm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8k5mqutwgcswho5jwcm.png" alt="City Skyline Day" width="800" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lpc2xqdjqb6gtmuhx1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lpc2xqdjqb6gtmuhx1p.png" alt="City Skyline Night" width="800" height="687"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can have a look at the code by accessing this repo:&lt;br&gt;
&lt;a href="https://github.com/SwarnaliRoy94/FreeCodeCamp-CSS-Projects/tree/main/CSSVariablesSkyline" rel="noopener noreferrer"&gt;https://github.com/SwarnaliRoy94/FreeCodeCamp-CSS-Projects/tree/main/CSSVariablesSkyline&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you have learned something new reading this blog. Thank you so much for your valuable time. Feel free to ask me anything in the discussion section.&lt;/p&gt;

&lt;p&gt;Happy Coding! Happy Learning!!&lt;/p&gt;

</description>
      <category>css</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The keyword "new" in JavaScript</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Sun, 19 Sep 2021 17:44:58 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/the-keyword-new-in-javascript-fh6</link>
      <guid>https://forem.com/swarnaliroy94/the-keyword-new-in-javascript-fh6</guid>
      <description>&lt;p&gt;Hello Readers! &lt;br&gt;
Let me introduce you all with something new today! &lt;/p&gt;

&lt;p&gt;The topic today is the JavaScript Keyword &lt;em&gt;"new"&lt;/em&gt;! In JavaScript, the keyword &lt;em&gt;new&lt;/em&gt; is used to create &lt;em&gt;an instance of an object&lt;/em&gt; that has a &lt;em&gt;constructor function&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Before diving into the details, we need to understand one thing, that is, in JavaScript almost everything is an &lt;em&gt;Object&lt;/em&gt; and the concept of Object is very important to understand the keyword "New". When we define any &lt;em&gt;Boolean&lt;/em&gt;, &lt;em&gt;Number&lt;/em&gt; or &lt;em&gt;String&lt;/em&gt; with the keyword New, it can be treated like an &lt;em&gt;object&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let me show an example first!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb23ubt0a858w4v28gnwt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb23ubt0a858w4v28gnwt.png" alt="Construction Function" width="666" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 This is a "&lt;em&gt;constructor function&lt;/em&gt;" , &lt;em&gt;Animal&lt;/em&gt; since it is responsible for &lt;em&gt;constructing&lt;/em&gt; a new object, &lt;em&gt;animal&lt;/em&gt;.&lt;br&gt;
👉 We had to add properties to the animal object with the dot notation and return it from the constructor function.&lt;br&gt;
👉 Let's assume that we need to create more than one Animal object in our application. So, we instantiated the constructor function twice.&lt;br&gt;
👉 We can see the output where two new animal objects are created with &lt;em&gt;name&lt;/em&gt; and &lt;em&gt;legs&lt;/em&gt; properties.&lt;/p&gt;

&lt;p&gt;Here is the interesting thing about the keyword &lt;em&gt;new&lt;/em&gt;. When we use the keyword, a property called &lt;em&gt;this&lt;/em&gt; is created which refers to the whole object and is returned automatically. So, we don't need to specify any additional object inside our constructor function. Basically, those two lines are done for us implicitly (&lt;em&gt;under the hood&lt;/em&gt;). &lt;/p&gt;

&lt;p&gt;Let's take a look what happens &lt;em&gt;under the hood&lt;/em&gt; , assuming the Animal constructor is called with the &lt;em&gt;new&lt;/em&gt; keyword. It can be re-written as following and it is equivalent to the previous example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nhzmhznfdtwtuwffi6k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nhzmhznfdtwtuwffi6k.png" alt="With Comments" width="666" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, a new object is automatically created and returned. (the commented out lines)&lt;/p&gt;

&lt;p&gt;We can compactly write the code without the &lt;em&gt;under the hood&lt;/em&gt; comments:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ux9qbqd1elu7u6ps19h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ux9qbqd1elu7u6ps19h.png" alt="Without Comments" width="666" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This concept is known as "&lt;strong&gt;Pseudoclassical Instantiation&lt;/strong&gt;". &lt;/p&gt;

&lt;p&gt;Similarly, we can define an &lt;em&gt;Object property&lt;/em&gt; which is itself another &lt;em&gt;Object&lt;/em&gt;! &lt;/p&gt;

&lt;p&gt;For &lt;em&gt;example&lt;/em&gt;, let's define an object called "&lt;em&gt;Human&lt;/em&gt;" And then instantiate two new &lt;em&gt;Human&lt;/em&gt; objects as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5wa2wzr2wwdelq8ff9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5wa2wzr2wwdelq8ff9k.png" alt="Human" width="666" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, suppose we want to create an object type for cars and name it "&lt;em&gt;Car&lt;/em&gt;". It should have properties named &lt;em&gt;owner&lt;/em&gt;, &lt;em&gt;model&lt;/em&gt; and &lt;em&gt;yearOfPurchase&lt;/em&gt;. Instead of passing a &lt;em&gt;literal string&lt;/em&gt; or &lt;em&gt;integer value&lt;/em&gt; while creating the new objects, the following statements pass the objects &lt;em&gt;man&lt;/em&gt; and &lt;em&gt;woman&lt;/em&gt; as the parameters for the owners. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh0x2vc3gvjiphkitk73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh0x2vc3gvjiphkitk73.png" alt="Car" width="666" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To find out the name of the owner of car2, we can access the property as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Swarnali&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Hope this post help you while creating a lot of similar objects in your application. Discussion and queries are always welcomed! ☺️☺️
&lt;/h6&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Reverse a String in JavaScript</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Mon, 06 Sep 2021 09:51:05 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/reverse-a-string-in-javascript-e1i</link>
      <guid>https://forem.com/swarnaliroy94/reverse-a-string-in-javascript-e1i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Reversing a String&lt;/strong&gt; is indeed one of the most common and needed operations in JavaScript. During the journey of any Software Developer's career, a very important and basic question for interviews is &lt;em&gt;"How to Reverse a String in JavaScript"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are a few ways to &lt;em&gt;reverse a string&lt;/em&gt; in JavaScript. We can use &lt;em&gt;loops&lt;/em&gt;, &lt;em&gt;built-in functions&lt;/em&gt;, &lt;em&gt;recursion&lt;/em&gt; and even &lt;em&gt;regular expressions&lt;/em&gt; to solve the problem. In this post, I am going to show these approaches with examples. So, let's get started!! &lt;/p&gt;

&lt;h4&gt;
  
  
  🔶built-in Methods: split().reverse().join()
&lt;/h4&gt;

&lt;p&gt;The very first way that I want to discuss is perhaps the most commonly used way, which is using the &lt;em&gt;built-in methods&lt;/em&gt;. First of all, we need to split the string to an Array of Single Characters i.e.("s","t","r","i","n","g"), then reverse the characters and lastly join them again to create the &lt;em&gt;reversed string&lt;/em&gt;. &lt;br&gt;
Let's see an example first:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ktboawa1l3ninfea6af.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ktboawa1l3ninfea6af.png" alt="split().reverse().join()" width="428" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, basically &lt;em&gt;three built-in methods&lt;/em&gt; are used. They are : &lt;code&gt;String.prototype.split()&lt;/code&gt;, &lt;code&gt;Array.prototype.reverse()&lt;/code&gt; &amp;amp; &lt;code&gt;Array.prototype.join()&lt;/code&gt;. To understand it properly, I am explaining it elaborately.&lt;/p&gt;

&lt;p&gt;🔸 &lt;code&gt;String.prototype.split()&lt;/code&gt; method &lt;em&gt;splits&lt;/em&gt; the &lt;em&gt;String object&lt;/em&gt; into an &lt;em&gt;Array of String&lt;/em&gt; by separating the string into sub strings. In this case, &lt;em&gt;stringToReverse.split("")&lt;/em&gt; returns the output as: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;['S', 'o', 'f', 't', 'w','a', 'r', 'e', ' ', 'D', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 After the string is separated as an array of string, the &lt;code&gt;Array.prototype.reverse()&lt;/code&gt; does the main work which is reversing the single array elements in place. The &lt;em&gt;first array element&lt;/em&gt; is now the &lt;em&gt;last array element&lt;/em&gt; and vice-versa. &lt;br&gt;
In our case, &lt;em&gt;splitString.reverse()&lt;/em&gt; returns the output as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;['r', 'e', 'p', 'o', 'l', 'e', 'v', 'e', 'D', ' ', 'e', 'r', 'a', 'w', 't', 'f', 'o', 'S']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 The &lt;code&gt;Array.prototype.join()&lt;/code&gt; method finally rejoins all the single characters previously separated by the &lt;em&gt;split()&lt;/em&gt; method to recompose the &lt;em&gt;reversed string&lt;/em&gt;. In our example the final output is: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;repoleveD erawtfoS&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Chaining these Methods
&lt;/h4&gt;

&lt;p&gt;Well, these three methods can be chained to make our code compact and clean in the following way and the &lt;em&gt;output&lt;/em&gt; will be the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3t77prsy6t8sa0iyf07.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3t77prsy6t8sa0iyf07.jpg" alt="Chaining methods together" width="682" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However,instead of using &lt;code&gt;String.prototype.split()&lt;/code&gt;, we can do this work using &lt;code&gt;Spread Operator [...]&lt;/code&gt; also, which is a &lt;em&gt;ES6 syntax&lt;/em&gt;. It works the same way as before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fch7nqwd1ncgydw36xtde.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fch7nqwd1ncgydw36xtde.jpg" alt="Using spread operator" width="665" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, the &lt;em&gt;spread operator&lt;/em&gt; is exactly doing the same work as &lt;em&gt;split()&lt;/em&gt; does, it is splitting the string object into single characters.&lt;/p&gt;
&lt;h4&gt;
  
  
  🔶Using Decrementing For Loop
&lt;/h4&gt;

&lt;p&gt;This is an easy and oldest approach of reversing a string in JavaScript but works pretty well everywhere.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6pkihhjh9z9eu4cab6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6pkihhjh9z9eu4cab6v.png" alt="For Loop" width="517" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔸 At first, &lt;em&gt;newString&lt;/em&gt; created an &lt;em&gt;empty string&lt;/em&gt; to host the &lt;em&gt;new created string&lt;/em&gt;.&lt;br&gt;
🔸 The starting point of the loop will be &lt;em&gt;(str.length - 1)&lt;/em&gt; which corresponds to the &lt;em&gt;last character&lt;/em&gt; of the string, "r". As long as &lt;em&gt;i&lt;/em&gt; is &lt;em&gt;greater than or equals zero&lt;/em&gt;, the loop will go on. We are decrementing &lt;em&gt;i&lt;/em&gt; after each iteration.&lt;br&gt;
🔸 The function then &lt;em&gt;returns&lt;/em&gt; the &lt;em&gt;reversed string&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can also use the &lt;code&gt;for loop&lt;/code&gt; syntax introduced by &lt;em&gt;JavaScript ES6&lt;/em&gt; which is also very easy to use and decreases the chances of mistake while writing the conditions of the loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45ok7anh1sghu0uvbo4u.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45ok7anh1sghu0uvbo4u.jpg" alt="For loop ES6" width="518" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Much cleaner than the previous &lt;em&gt;for loop&lt;/em&gt;, isn't it?? But works really fine! &lt;/p&gt;

&lt;p&gt;🔸 The "c" in the for loop condition is taking each of the letter of the string as a single character. For better understanding I have added two console.log() statement in the following picture and also you can see the output in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4svsioj0uxutsrxnifss.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4svsioj0uxutsrxnifss.jpg" alt="For of loop with console" width="443" height="880"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  🔶The Recursive Approach: Recursion Method
&lt;/h4&gt;

&lt;p&gt;Using the &lt;em&gt;Recursion method&lt;/em&gt; is another very well-known approach to reverse a string in JavaScript. We need &lt;em&gt;two methods&lt;/em&gt; to perform this recursive approach. One is the &lt;code&gt;String.prototype.substr()&lt;/code&gt; method and another is the &lt;code&gt;String.prototype.charAt()&lt;/code&gt; method.&lt;br&gt;
Let's see an &lt;em&gt;example&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7hi82myi68ha9gsv99x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7hi82myi68ha9gsv99x.jpg" alt="Recursive Approach" width="632" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔸 &lt;em&gt;String.prototype.substr()&lt;/em&gt; method returns a &lt;em&gt;portion of the string&lt;/em&gt;, beginning at the &lt;em&gt;specified index&lt;/em&gt; and extending for a given number of characters afterwards. &lt;br&gt;
In our example, the part &lt;code&gt;str.substr(1)&lt;/code&gt; returns &lt;em&gt;"ecursion"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔸 &lt;code&gt;String.prototype.charAt()&lt;/code&gt; method returns the &lt;em&gt;specified character&lt;/em&gt; from a string.&lt;br&gt;
In our example, the part &lt;code&gt;str.charAt(0)&lt;/code&gt; returns &lt;em&gt;"R"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔸 &lt;code&gt;reverseString(str.substr(1)) + str.charAt(0)&lt;/code&gt; firstly returns the &lt;em&gt;portion&lt;/em&gt; of the string , starting at the index of the first character to include in the returned substring . Second part of the method hits the &lt;em&gt;if condition&lt;/em&gt; and the most highly nested call returns immediately.&lt;br&gt;
We need to remember that this method &lt;em&gt;won’t have just one call&lt;/em&gt; but have &lt;em&gt;several nested calls&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Nevertheless, this approach is not a best approach for reversing a string as the &lt;em&gt;depth&lt;/em&gt; of the &lt;em&gt;recursion&lt;/em&gt; is equal to the &lt;em&gt;length&lt;/em&gt; of the string and in case of a very long string, it takes much more time than any other method and the &lt;em&gt;size of the stack&lt;/em&gt; is a major concern here. &lt;/p&gt;
&lt;h4&gt;
  
  
  🔶 Using Array.prototype.reduce() Method
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Array.prototype.reduce()&lt;/code&gt; method &lt;em&gt;executes&lt;/em&gt; a &lt;em&gt;reducer&lt;/em&gt; callback function on each element of the array, passing in the return value from the calculation on the preceding element and &lt;em&gt;returns&lt;/em&gt; a &lt;em&gt;single value&lt;/em&gt; as the &lt;em&gt;final result&lt;/em&gt;. The syntax can be written as: &lt;br&gt;
&lt;code&gt;reduce((previousValue, currentValue) =&amp;gt; { ... } )&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's see an &lt;em&gt;example&lt;/em&gt; of this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringReversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stringReversed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Swarnali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//ilanrawS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔸 The &lt;em&gt;reverseString&lt;/em&gt; function takes a string &lt;em&gt;str&lt;/em&gt; as parameter. &lt;br&gt;
🔸 The first thing that we need to do is to split the string into single characters. We took an array &lt;em&gt;arr&lt;/em&gt; to hold the values. &lt;br&gt;
🔸 reduce() function takes two parameters, &lt;em&gt;reversed&lt;/em&gt; and &lt;em&gt;character&lt;/em&gt;. If we compare it with the basic syntax of reduce(), &lt;em&gt;reversed&lt;/em&gt; is the previous value/accumulator and &lt;em&gt;character&lt;/em&gt; is the current value. The function stringReversed returns the current value adding it with the previous value, which is actually reversing the whole array characters and joining them together in a reversed way. &lt;/p&gt;

&lt;p&gt;This code block can be more compact if we use JavaScript ES6 syntax. 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reverseString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Roy is a Developer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//repoleveD a si yoR&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making it a one-line code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reverseString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Roy is a developer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//repoleveD a si yoR&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  🔶Using Regular Expressions (RegEx)
&lt;/h4&gt;

&lt;p&gt;This is one of the &lt;em&gt;rarest and trickiest approaches&lt;/em&gt; of reversing a string but developers who love to play with &lt;em&gt;JavaScript Regular Expressions&lt;/em&gt; can definitely try this approach. Also, in any interview, if you can show this, that might be a &lt;em&gt;plus point&lt;/em&gt; as it is such an approach that people generally don't use and they will know that you have mastered another skill that is &lt;em&gt;Regular Expressions&lt;/em&gt;! &lt;br&gt;
The following is an example of this approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My name is Swarnali Roy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/.&lt;/span&gt;&lt;span class="se"&gt;{1,1}&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reveresed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reveresed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//yoR ilanrawS si eman yM&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔸The simplest &lt;em&gt;quantifier&lt;/em&gt; in RegEx is a &lt;em&gt;number in curly braces: {n}&lt;/em&gt;. A quantifier is appended to a character (or a character class, or a [...] set etc) and specifies how many we need.&lt;br&gt;
In our example, {1,1} denotes we &lt;em&gt;exactly&lt;/em&gt; need 1 character to be matched. If we write console.log(result), then we will get something like this:&lt;br&gt;
&lt;code&gt;['M', 'y', ' ', 'n', 'a','m', 'e', ' ', 'i', 's', ' ', 'S', 'w', 'a', 'r', 'n', 'a', 'l', 'i', ' ','R', 'o', 'y']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 The &lt;em&gt;RegEx&lt;/em&gt; here is mainly doing the work of separating the string object into &lt;em&gt;single characters of an array&lt;/em&gt;. After separating the reverse() and join() method is working exactly like it worked with &lt;code&gt;split()&lt;/code&gt; or &lt;code&gt;spread operator&lt;/code&gt; as I have shown above in the &lt;em&gt;built-in approach&lt;/em&gt;.&lt;/p&gt;

&lt;h6&gt;
  
  
  I hope the readers understood the tutorial and it can help you in your interviews as well. Queries are always welcomed in the discussion section.
&lt;/h6&gt;

</description>
      <category>javascript</category>
      <category>career</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Introduction to React : Real DOM &amp; Virtual DOM</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Tue, 31 Aug 2021 15:20:36 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/introduction-to-react-real-dom-virtual-dom-363</link>
      <guid>https://forem.com/swarnaliroy94/introduction-to-react-real-dom-virtual-dom-363</guid>
      <description>&lt;p&gt;Hello Readers!&lt;/p&gt;

&lt;p&gt;I have been writing posts about &lt;em&gt;JavaScript&lt;/em&gt; for a couple of months now. A strong foundation of JavaScript is a &lt;em&gt;prerequisite&lt;/em&gt; for learning &lt;em&gt;React.JS&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I have been working and developing projects with &lt;em&gt;React.JS&lt;/em&gt; for the last one year and I have found it very flexible and useful for building user interfaces and process of developing web applications becomes easier with React.JS. &lt;/p&gt;

&lt;p&gt;First of all, let's have a look on what React.JS is and why it is so familiar nowadays among developers. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is React?
&lt;/h3&gt;

&lt;p&gt;◾ React is an efficient, flexible and open-source &lt;em&gt;JavaScript Library&lt;/em&gt; for building simple and scalable front-ends, created by &lt;em&gt;Facebook&lt;/em&gt;.&lt;br&gt;
◾ It is basically used to build &lt;em&gt;Single Page Applications&lt;/em&gt;. It also allows us to create &lt;em&gt;reusable&lt;/em&gt; UI components. &lt;br&gt;
◾ React.JS is &lt;em&gt;not a framework&lt;/em&gt; of JavaScript like Angular or React Native.&lt;br&gt;
◾ It is the &lt;em&gt;View&lt;/em&gt; layer of &lt;em&gt;MVC (Model View Controller)&lt;/em&gt; application.&lt;/p&gt;
&lt;h3&gt;
  
  
  How Does React Work?
&lt;/h3&gt;

&lt;p&gt;Instead of manipulating the browser's real DOM directly, React creates a virtual DOM in memory. React finds out what &lt;em&gt;changes&lt;/em&gt; are made and only manipulates the &lt;em&gt;virtual DOM&lt;/em&gt; before changing anything in the &lt;em&gt;real/browser's DOM&lt;/em&gt;.&lt;br&gt;
To understand it in a better way, we need to understand the difference between Real/Browser's DOM and React's virtual DOM.&lt;/p&gt;
&lt;h4&gt;
  
  
  What is Real/Browser DOM?
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;DOM&lt;/em&gt; stands for &lt;em&gt;"Document Object Model"&lt;/em&gt;. It represents the entire UI of the &lt;em&gt;web application&lt;/em&gt; as a &lt;em&gt;tree data structure&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In simpler terms, it is a &lt;em&gt;structural representation of the HTML elements&lt;/em&gt; of the web application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj7waqg382e8txgf28mq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj7waqg382e8txgf28mq.jpg" alt="Real/Browser DOM image" width="600" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whenever there is a change in the &lt;em&gt;state of application UI&lt;/em&gt;, the DOM gets &lt;em&gt;updated&lt;/em&gt; and represents that change. With every change, the DOM gets manipulated and &lt;em&gt;re-rendered&lt;/em&gt; to update the application UI, which affects the performance and makes it slower.&lt;/p&gt;

&lt;p&gt;Hence, with more UI components and complex structure of the DOM, the DOM updates will be more costly as with every change it needs to be re-rendered.  &lt;/p&gt;
&lt;h4&gt;
  
  
  What is React's Virtual DOM?
&lt;/h4&gt;

&lt;p&gt;To make the performance of the Real DOM better and faster, the concept of Virtual DOM arrives. The Virtual DOM is nothing but the virtual representation of the DOM. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But the key difference is, every time with every change , the virtual DOM gets updated instead of the real DOM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like, &lt;em&gt;real DOM&lt;/em&gt; , &lt;em&gt;virtual DOM&lt;/em&gt; is also represented as a &lt;em&gt;tree structure&lt;/em&gt;. Each element is a &lt;em&gt;node&lt;/em&gt; in this tree. When a &lt;em&gt;new item&lt;/em&gt; is added to the application UI, a node is added to the tree as well. If the &lt;em&gt;state&lt;/em&gt; of any of these elements changes, a &lt;em&gt;new&lt;/em&gt; virtual DOM tree is created. The virtual DOM &lt;em&gt;computes&lt;/em&gt; the best possible way or we can say the &lt;em&gt;minimal operations&lt;/em&gt; on the real DOM to make changes to the real DOM. Thus, it makes efficient and better performance by reducing the cost and operations of re-rendering the whole real DOM. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj6ybb6v0bpitt2384cq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj6ybb6v0bpitt2384cq2.png" alt="Virtual DOM Image" width="670" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the basic understanding of Real and Virtual DOM, let's focus on how &lt;em&gt;React&lt;/em&gt; works using the &lt;em&gt;Virtual DOM&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔸 In React, each UI is an individual &lt;em&gt;component&lt;/em&gt; and each component has it's own &lt;em&gt;state&lt;/em&gt;. &lt;br&gt;
🔸 React follows the &lt;em&gt;observable pattern&lt;/em&gt; and observes the changes of the states.&lt;br&gt;
🔸 Whenever a change is made in the state of any component, React &lt;em&gt;updates&lt;/em&gt; the &lt;em&gt;virtual DOM tree&lt;/em&gt; but does not change the &lt;em&gt;real DOM tree&lt;/em&gt;&lt;br&gt;
🔸 After &lt;em&gt;updating&lt;/em&gt;, React then &lt;em&gt;compares&lt;/em&gt; the &lt;em&gt;current version&lt;/em&gt; of the &lt;em&gt;virtual DOM&lt;/em&gt; with the &lt;em&gt;previous version&lt;/em&gt;.&lt;br&gt;
🔸 React knows which &lt;em&gt;objects&lt;/em&gt; are changed in the &lt;em&gt;virtual DOM&lt;/em&gt;, based on that it &lt;em&gt;only&lt;/em&gt; changes those objects in the &lt;em&gt;Real DOM&lt;/em&gt;, making &lt;em&gt;minimum&lt;/em&gt; manipulating operations.&lt;br&gt;
🔸 This process is noted as &lt;em&gt;"diffing"&lt;/em&gt;. A picture below will clear the concept more. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fux2o2yg5lfrvwut60spp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fux2o2yg5lfrvwut60spp.png" alt="Diffing Process (source: https://programmingwithmosh.com" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this image, the &lt;em&gt;red circles&lt;/em&gt; are the &lt;em&gt;nodes&lt;/em&gt; that has been changed. That means, the &lt;em&gt;state&lt;/em&gt; of these components are changed. React computes the difference of the previous and current version of the &lt;em&gt;Virtual DOM tree&lt;/em&gt; and the whole parent sub-tree gets re-rendered to show the changed UI. Then the updated tree is &lt;em&gt;batch updated&lt;/em&gt; (This means that updates to the real DOM are sent in batches, instead of sending updates for every single change in state.) to the Real DOM.&lt;/p&gt;

&lt;p&gt;To get even deeper knowledge about it, we need to know about &lt;code&gt;React render() function&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Prior to that, we need to know about some most important &lt;em&gt;features&lt;/em&gt; of React. I will not dive into deeper knowledge about it as I will write elaborately in other separate posts later. But for now what we need to know is: &lt;/p&gt;

&lt;p&gt;🔹 JSX&lt;br&gt;
&lt;em&gt;JSX&lt;/em&gt; stands for &lt;em&gt;JavaScript XML&lt;/em&gt;. It is a &lt;em&gt;syntax extension&lt;/em&gt; of JS. By using JSX, we can write &lt;em&gt;HTML structures&lt;/em&gt; in the same file that contains &lt;em&gt;JavaScript code&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔹 Components&lt;br&gt;
Each UI in a React app is a &lt;em&gt;component&lt;/em&gt;. Components are &lt;em&gt;independent&lt;/em&gt; and &lt;em&gt;reusable&lt;/em&gt; bits of code. A single application may have multiple &lt;em&gt;components&lt;/em&gt;. &lt;br&gt;
Components can be of two types, &lt;em&gt;Class Components&lt;/em&gt; &amp;amp; &lt;em&gt;Functional Components&lt;/em&gt;. &lt;code&gt;Class Components&lt;/code&gt; are &lt;em&gt;stateful&lt;/em&gt; as they can use their &lt;em&gt;"state"&lt;/em&gt; to change the UI. &lt;code&gt;Functional Components&lt;/code&gt; are &lt;em&gt;stateless&lt;/em&gt; components. They act as a &lt;em&gt;JavaScript function&lt;/em&gt; that can take an &lt;em&gt;arbitrary parameter&lt;/em&gt; called &lt;em&gt;"props"&lt;/em&gt;. &lt;br&gt;
Recently, &lt;em&gt;React Hooks&lt;/em&gt; are introduced to use state within the Functional Components. &lt;/p&gt;

&lt;p&gt;🔹 Lifecycle Methods&lt;br&gt;
&lt;em&gt;Lifecycle methods&lt;/em&gt; are special methods &lt;em&gt;built-in&lt;/em&gt; to React, used to operate on components throughout their duration in the DOM. Every component in React goes through a lifecycle of events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;render()&lt;/code&gt; method is the most used &lt;em&gt;lifecycle method&lt;/em&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;It is the only required method within the &lt;em&gt;React Class Components&lt;/em&gt;. So, &lt;code&gt;render()&lt;/code&gt; is called in every class component. &lt;br&gt;
&lt;em&gt;render() method&lt;/em&gt; handles the rendering of the component to the UI. The render() method contains all the logic that should be displayed on the screen. It might also contain a &lt;em&gt;null&lt;/em&gt; value, if we don’t want to show anything on the display.&lt;/p&gt;

&lt;p&gt;An &lt;em&gt;example&lt;/em&gt; is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Header&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="nx"&gt;Introduction&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example will show the JSX written inside the render() method.&lt;/p&gt;

&lt;p&gt;When a &lt;em&gt;state&lt;/em&gt; or &lt;em&gt;prop&lt;/em&gt; within the component is updated, the &lt;em&gt;render()&lt;/em&gt; will return a different tree of React elements. &lt;br&gt;
React then &lt;em&gt;calculates&lt;/em&gt; how to update the UI to match the most &lt;em&gt;recent tree changes&lt;/em&gt;. At first, React updates its &lt;em&gt;virtual DOM&lt;/em&gt; and updates only the object that have changed in the &lt;em&gt;real DOM&lt;/em&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  In the next posts, I'll write about the features of React in Details. If you like this post please save, like and press unicorn and also follow me to keep an eye on my other posts.
&lt;/h5&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Using RegEx Capturing Groups</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Fri, 27 Aug 2021 18:03:10 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/using-regex-capturing-groups-je2</link>
      <guid>https://forem.com/swarnaliroy94/using-regex-capturing-groups-je2</guid>
      <description>&lt;p&gt;Hello Readers!&lt;/p&gt;

&lt;p&gt;I always love to work with Regular Expressions and a very important concept of RegEx is &lt;em&gt;"RegEx Capturing Groups"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Sometimes in a string, &lt;em&gt;patterns&lt;/em&gt; we &lt;em&gt;search&lt;/em&gt; for may occur &lt;em&gt;multiple times&lt;/em&gt;. It is wasteful to manually &lt;em&gt;repeat&lt;/em&gt; that regex. A better way to specify when we have &lt;em&gt;multiple repeated substrings&lt;/em&gt; is using &lt;em&gt;"RegEx Capturing Groups"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;👉 Parentheses (  ), are used to find &lt;em&gt;repeated substrings&lt;/em&gt;. We just need to put the &lt;em&gt;regex&lt;/em&gt; that will repeat in between the parentheses. &lt;br&gt;
👉 It allows to get a part of the match as a &lt;em&gt;separate item&lt;/em&gt; in the result array.&lt;br&gt;
👉 If we put a &lt;em&gt;quantifier&lt;/em&gt; after the parentheses, it applies to the parentheses as a whole.&lt;/p&gt;

&lt;p&gt;Let's see an &lt;em&gt;example&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;go&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;+/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;goGoGOgOgoooogo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'goGoGOgOgo', 'go' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Parentheses ( ), group characters together, so (go)+ means &lt;em&gt;"go"&lt;/em&gt;, &lt;em&gt;"gogo"&lt;/em&gt;, "&lt;em&gt;gogogo&lt;/em&gt;" and so on. But once it found another o after "go", it stopped matching the regex. Then again, when it found another &lt;em&gt;"go"&lt;/em&gt; , it returned a second match.&lt;/p&gt;

&lt;h4&gt;
  
  
  Specify RegEx Capturing Groups using Numbers
&lt;/h4&gt;

&lt;p&gt;Let's say, we need to write a pattern which will repeat more than once in a string. In that case, we can easily specify it without writing the pattern again and again.&lt;/p&gt;

&lt;p&gt;To specify where that repeat string will appear, you need to use a backslash () and then a number. This number starts at 1 and increases with each additional capture group you want to use. &lt;/p&gt;

&lt;p&gt;🔸 Example: The code block will &lt;em&gt;match&lt;/em&gt; a string that consists of only the &lt;em&gt;same number&lt;/em&gt; repeated exactly three times separated by single spaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;repeatNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;93 93 93&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wrongNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100 100 200 100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(\d&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s\1\s\1&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repeatNum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//true&lt;/span&gt;

&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;repeatNum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ '93 93 93' ]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wrongRes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrongNum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrongRes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//false&lt;/span&gt;

&lt;span class="nx"&gt;wrongRes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wrongNum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrongRes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/^(\d+)\s\1\s\1$/&lt;/code&gt; this regex explains: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(i)&lt;/em&gt; a &lt;em&gt;caret&lt;/em&gt; &lt;code&gt;( ^ )&lt;/code&gt; is at the &lt;em&gt;beginning&lt;/em&gt; of the entire &lt;em&gt;regular expression&lt;/em&gt;, it matches the &lt;em&gt;beginning of a line&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;(ii)&lt;/em&gt; &lt;code&gt;(\d+)&lt;/code&gt; is the first capturing group that finds &lt;em&gt;any digit from 0-9&lt;/em&gt; appears at least &lt;em&gt;one or more times&lt;/em&gt; in the string.&lt;br&gt;
&lt;em&gt;(iii)&lt;/em&gt; &lt;code&gt;\s&lt;/code&gt; finds a &lt;em&gt;single white space&lt;/em&gt;&lt;br&gt;
&lt;em&gt;(iv)&lt;/em&gt; &lt;code&gt;\1&lt;/code&gt; represents the first capturing group which is &lt;code&gt;(\d+)&lt;/code&gt;.&lt;br&gt;
&lt;em&gt;(v)&lt;/em&gt; a &lt;em&gt;dollar sign ( $ )&lt;/em&gt; is at the &lt;em&gt;end&lt;/em&gt; of the entire &lt;em&gt;regular expression&lt;/em&gt;, it matches the &lt;em&gt;end of a line&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔹 The &lt;em&gt;first capturing group&lt;/em&gt; is repeated with * \1 * and separated by &lt;em&gt;white space&lt;/em&gt;. The output will match any three same numbers like &lt;em&gt;"100 100 100"&lt;/em&gt;, &lt;em&gt;"93 93 93"&lt;/em&gt; but will not match more than 3 times! &lt;br&gt;
🔹 &lt;code&gt;regex.test(repeatNum)&lt;/code&gt; returns true and matches "93 93 93" but &lt;code&gt;regex.test(wrongNum)&lt;/code&gt; returns false and as it doesn't find any match, .match() returns &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  RegEx Capturing Groups to &lt;em&gt;Search and Replace&lt;/em&gt; Text in a String using string.replace()
&lt;/h4&gt;

&lt;p&gt;In &lt;em&gt;regular expressions&lt;/em&gt;, we can make &lt;em&gt;searching&lt;/em&gt; even more powerful when it also &lt;em&gt;replaces&lt;/em&gt; the text we need to &lt;em&gt;match&lt;/em&gt;.&lt;br&gt;
&lt;code&gt;string.replace()&lt;/code&gt; is the desired method to &lt;em&gt;search and replace&lt;/em&gt; any pattern in that string. &lt;/p&gt;

&lt;p&gt;👉 It requires &lt;em&gt;two parameters&lt;/em&gt;. &lt;br&gt;
👉 First is the &lt;em&gt;regex pattern&lt;/em&gt; we want to search for.&lt;br&gt;
👉 Second is the &lt;em&gt;string to replace&lt;/em&gt; the match or a &lt;em&gt;function&lt;/em&gt; to do something. &lt;br&gt;
👉 &lt;em&gt;Capturing groups&lt;/em&gt; can be accessed in the &lt;em&gt;replacement string&lt;/em&gt; with &lt;em&gt;dollar signs ($)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🔸 Example: The &lt;em&gt;"str" string&lt;/em&gt; includes three different words. We need to capture &lt;em&gt;three different groups&lt;/em&gt;. A regex &lt;em&gt;"fixRegex"&lt;/em&gt; is used for capturing &lt;em&gt;three capture groups&lt;/em&gt; that will search for &lt;em&gt;each word&lt;/em&gt; in the string. Then update the &lt;em&gt;"replaceText"&lt;/em&gt; variable to &lt;em&gt;replace&lt;/em&gt; the string &lt;em&gt;"one two three"&lt;/em&gt; with the string &lt;em&gt;"three two one"&lt;/em&gt; and assign the result to the &lt;em&gt;result&lt;/em&gt; variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one two three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fixRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;replaceText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$3 $2 $1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fixRegex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;replaceText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//three two one&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The regex &lt;code&gt;/^(\w+)\s(\w+)\s(\w+)$/&lt;/code&gt; explains:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(i)&lt;/em&gt; a &lt;em&gt;caret ( ^ )&lt;/em&gt; is at the &lt;em&gt;beginning&lt;/em&gt; of the entire &lt;em&gt;regular expression&lt;/em&gt;, it matches the &lt;em&gt;beginning of a line&lt;/em&gt;. &lt;br&gt;
&lt;em&gt;(ii)&lt;/em&gt; &lt;code&gt;\w&lt;/code&gt; means Find a word character including _ &lt;br&gt;
&lt;em&gt;(iii)&lt;/em&gt; &lt;code&gt;+&lt;/code&gt; after &lt;code&gt;\w&lt;/code&gt; means find a word character including _ that matches any string that contains &lt;em&gt;at least one word character&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;(iv)&lt;/em&gt; &lt;code&gt;\s&lt;/code&gt; means Find a &lt;em&gt;white space&lt;/em&gt; character.&lt;br&gt;
&lt;em&gt;(v)&lt;/em&gt; a &lt;em&gt;dollar sign ( $ )&lt;/em&gt; is at the &lt;em&gt;end&lt;/em&gt; of the entire &lt;em&gt;regular expression&lt;/em&gt;, it matches the &lt;em&gt;end of a line&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As there are &lt;em&gt;three different capturing groups&lt;/em&gt;, so &lt;em&gt;\1&lt;/em&gt; after &lt;em&gt;\s&lt;/em&gt; will not work as it will repeat the word &lt;em&gt;"one"&lt;/em&gt;. We need to write &lt;em&gt;(\w+)&lt;/em&gt; three times. &lt;/p&gt;

&lt;p&gt;🔹 The &lt;em&gt;"replaceText"&lt;/em&gt; replaced the &lt;em&gt;1st capturing group&lt;/em&gt; with the &lt;em&gt;3rd capturing group&lt;/em&gt; which is simply done with the &lt;em&gt;dollar sign ($)&lt;/em&gt;. &lt;br&gt;
🔹 $3 captures the &lt;em&gt;3rd group&lt;/em&gt; which is the word &lt;em&gt;"three"&lt;/em&gt; and replaces it with the &lt;em&gt;1st group&lt;/em&gt; 1 which is &lt;em&gt;"one"&lt;/em&gt; using $1. &lt;br&gt;
🔹 The &lt;em&gt;2nd group&lt;/em&gt; remains as it is denoted by $2.&lt;br&gt;
🔹 The &lt;code&gt;string.replace()&lt;/code&gt; method took the &lt;em&gt;fixRegex&lt;/em&gt; as &lt;em&gt;first parameter&lt;/em&gt; and &lt;em&gt;replaceText&lt;/em&gt; as the &lt;em&gt;second parameter&lt;/em&gt; and simply returned &lt;em&gt;"three two one"&lt;/em&gt; replacing one and three with each other. &lt;/p&gt;

&lt;h5&gt;
  
  
  If you find it interesting, write a regex to solve this problem. Replace the following string with &lt;em&gt;"five three one 6 4 2"&lt;/em&gt; using &lt;em&gt;RegEx Capturing Groups&lt;/em&gt; and &lt;em&gt;string.replace&lt;/em&gt; method.
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one 2 three 4 five 6&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//output: five three one 6 4 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Questions are always welcomed in the discussion section!!
&lt;/h6&gt;

</description>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Array.splice() &amp; Array.slice()</title>
      <dc:creator>Swarnali Roy</dc:creator>
      <pubDate>Tue, 17 Aug 2021 18:17:13 +0000</pubDate>
      <link>https://forem.com/swarnaliroy94/array-splice-array-slice-ag</link>
      <guid>https://forem.com/swarnaliroy94/array-splice-array-slice-ag</guid>
      <description>&lt;p&gt;Hello Dear Readers,&lt;/p&gt;

&lt;p&gt;In the previous posts of this series, we've already learnt how to &lt;em&gt;add&lt;/em&gt; elements to an array using &lt;code&gt;array.unshift()&lt;/code&gt; &amp;amp; &lt;code&gt;array.push()&lt;/code&gt; methods and &lt;em&gt;remove&lt;/em&gt; elements from an array using &lt;code&gt;array.shift()&lt;/code&gt; &amp;amp; &lt;code&gt;array.pop()&lt;/code&gt; methods. &lt;br&gt;
&lt;a href="https://dev.to/swarnaliroy94/add-items-to-arrays-with-unshift-push-methods-3ma2"&gt;https://dev.to/swarnaliroy94/add-items-to-arrays-with-unshift-push-methods-3ma2&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/swarnaliroy94/remove-items-from-arrays-with-shift-pop-methods-5caf"&gt;https://dev.to/swarnaliroy94/remove-items-from-arrays-with-shift-pop-methods-5caf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, what if we want &lt;em&gt;one or more than one&lt;/em&gt; item to be removed from somewhere in the &lt;em&gt;middle&lt;/em&gt; of an array, that too at the same time? &lt;br&gt;
Well, that's exactly where &lt;code&gt;array.splice()&lt;/code&gt; comes in. &lt;/p&gt;
&lt;h3&gt;
  
  
  Array.splice() Method
&lt;/h3&gt;

&lt;p&gt;👉 This method allows us to &lt;em&gt;remove&lt;/em&gt; any number of &lt;em&gt;consecutive elements&lt;/em&gt; from &lt;em&gt;anywhere&lt;/em&gt; in an array and/or &lt;em&gt;add&lt;/em&gt; new elements in place. &lt;br&gt;
👉 &lt;em&gt;Array.splice()&lt;/em&gt; can take up to three parameters.&lt;br&gt;
👉 We can use this method multiple times in a single code snippet , step by step. With each execution , the values of the indices change.&lt;/p&gt;

&lt;p&gt;The basic syntax are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;itemN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's know about these parameters and what they mean.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parameters of Array.splice()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 splice()'s &lt;em&gt;first parameter&lt;/em&gt; represents the &lt;em&gt;index&lt;/em&gt; on the array from which to &lt;em&gt;begin removing elements&lt;/em&gt;.&lt;br&gt;
🔸 If greater than the length of the array, &lt;code&gt;start&lt;/code&gt; will be set to the &lt;em&gt;length of the array&lt;/em&gt;. In this case, no element will be removed but the method will behave as an &lt;em&gt;adding function&lt;/em&gt;, adding as many element as provided. &lt;br&gt;
🔸 splice() can also take &lt;em&gt;negative integers&lt;/em&gt; as parameter. If negative, it will begin that many elements from the &lt;em&gt;end of the array&lt;/em&gt;. In this case, -1 means -n , that is the index of the &lt;em&gt;nth last element&lt;/em&gt;, and is therefore equivalent to the index of &lt;em&gt;array.length - n&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;deleteCount&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 The &lt;em&gt;second parameter&lt;/em&gt; indicates the number of elements to be &lt;em&gt;removed&lt;/em&gt; or &lt;em&gt;replaced&lt;/em&gt;.&lt;br&gt;
🔸 It indicates the number of elements in the array to &lt;em&gt;remove&lt;/em&gt; from &lt;code&gt;start&lt;/code&gt;. &lt;br&gt;
🔸 If &lt;code&gt;deleteCount&lt;/code&gt; is &lt;em&gt;omitted&lt;/em&gt; or is &lt;em&gt;equal to or greater than&lt;/em&gt; the number of elements left in the array, starting at &lt;code&gt;start&lt;/code&gt;, then all the elements from &lt;code&gt;start&lt;/code&gt; to the end of the array will be &lt;em&gt;eliminated&lt;/em&gt;. &lt;br&gt;
🔸 If &lt;code&gt;deleteCount&lt;/code&gt; is 0 or &lt;em&gt;negative&lt;/em&gt;, no elements will be &lt;em&gt;removed&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;item1, item2, ...., itemN&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 The &lt;em&gt;third parameter&lt;/em&gt; of splice() is &lt;em&gt;optional&lt;/em&gt; and &lt;em&gt;only&lt;/em&gt; used when we want to &lt;em&gt;add&lt;/em&gt; elements to the array, beginning from the &lt;code&gt;start&lt;/code&gt;.&lt;br&gt;
🔸 If no element is &lt;em&gt;specified&lt;/em&gt;, splice() will only &lt;em&gt;remove&lt;/em&gt; elements from the array. &lt;/p&gt;

&lt;p&gt;Let's move on to some &lt;em&gt;examples&lt;/em&gt; to understand this concept better. We need to always remember that index of an array starts at 0.&lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 1: Remove 2 elements starting from index 2 and insert two new elements.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orchid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;marigold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunflower&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;belly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'rose', 'orchid', 'belly' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'marigold', 'sunflower' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this example, &lt;code&gt;start&lt;/code&gt; is the index 2 and &lt;code&gt;deleteCount&lt;/code&gt; is also 2 that means, splice() will start removing elements from index 2 and remove the values of index 2 and 3.&lt;/p&gt;

&lt;p&gt;We can see the same example and insert two new elements &lt;em&gt;in place&lt;/em&gt; of the removed elements 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orchid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;marigold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunflower&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;belly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;almonda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rosemary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'rose', 'orchid', 'almonda', 'rosemary', 'belly' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'marigold', 'sunflower' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, value of index 2 and 3 are removed and &lt;em&gt;in place&lt;/em&gt; of them two new values are added.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Example 2: Remove elements using splice() multiple times
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orchid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;marigold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunflower&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;belly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;almonda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rosemary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hibiscus&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 'marigold' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// ['rose', 'orchid', 'sunflower', 'belly', 'almonda', 'rosemary', 'hibiscus' ]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removeAgain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removeAgain&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//[ 'rosemary', 'hibiscus' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// [ 'rose', 'orchid', 'sunflower', 'belly', 'almonda' ]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tulip&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//[ 'sunflower' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//[ 'rose', 'orchid', 'tulip', 'belly', 'almonda' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;example&lt;/em&gt; at first &lt;em&gt;marigold&lt;/em&gt; is removed from &lt;em&gt;index 2&lt;/em&gt;.&lt;br&gt;
Now, the modified array holds &lt;em&gt;rosemary&lt;/em&gt; and &lt;em&gt;hibiscus&lt;/em&gt; at index 5 and 6. So, removed them using *splice(5,2). Lastly, "sunflower" has been replaced by "tulip". &lt;br&gt;
In the process of changing the original array, with the first execution, the value of the indices has been changed and depending on that we executed the next steps. &lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 3: Remove 0 (zero) elements before index 3, and insert 1 new element.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rose&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orchid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;marigold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunflower&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;belly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;almonda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'rose', 'orchid', 'marigold', 'almonda', 'sunflower', 'belly' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: []&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, &lt;code&gt;deleteCount&lt;/code&gt; is 0 so nothing is removed but a new element is &lt;em&gt;added&lt;/em&gt; to the array at index 3 as the &lt;em&gt;first parameter&lt;/em&gt; of splice() is 3. &lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 4: Remove elements from index 0 and insert elements/nothing
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;birds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;crow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pigeon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parrot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;birds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;macaw&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;peacock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lovebird&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;birds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//output: [ 'macaw', 'peacock', 'lovebird', 'parrot' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'crow', 'pigeon' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's see what happens if we take the &lt;em&gt;same&lt;/em&gt; array and &lt;em&gt;remove&lt;/em&gt; all the elements and &lt;em&gt;insert&lt;/em&gt; nothing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;birds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;crow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pigeon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parrot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;birds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;instruments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: []&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'crow', 'pigeon', 'parrot' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, there were only 3 values and we &lt;em&gt;removed&lt;/em&gt; 3 items from index 0, so the &lt;em&gt;output&lt;/em&gt; shows an empty array.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Example 5: Remove elements from index -n
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;colours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pink&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colours&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colours&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'blue', 'black', 'red', 'grey' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'pink', 'yellow' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;start&lt;/code&gt; is -3 which indicates the &lt;em&gt;3rd last element&lt;/em&gt; of the array and &lt;code&gt;deleteCount&lt;/code&gt; is 2 which means the &lt;em&gt;splice()&lt;/em&gt; method will remove 2 elements from the 3rd last element and the other values will be same as before. &lt;/p&gt;

&lt;p&gt;Let's add some new values to the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;colours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colours&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pink&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;purple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colours&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'blue', 'black', 'pink', 'purple', 'grey' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'red' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;start&lt;/code&gt; is -2 which indicates the &lt;em&gt;2nd last element&lt;/em&gt; of the array and &lt;code&gt;deleteCount&lt;/code&gt; is 1 which means the &lt;em&gt;splice()&lt;/em&gt; method will remove 1 element from the 2nd last element and &lt;em&gt;add&lt;/em&gt; two new values starting from &lt;em&gt;index -2&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔹 Example 6: Remove all elements, no deleteCount, no new insertion
&lt;/h4&gt;

&lt;p&gt;Remove all elements starting from index 4 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;guitar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drums&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;violin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;piano&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tambourine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'guitar', 'drums', 'violin', 'piano' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'tambourine', 'flute' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remove all elements starting from index -4 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;guitar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drums&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;violin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;piano&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tambourine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'guitar', 'drums' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//output: [ 'violin', 'piano', 'tambourine', 'flute' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, we have known enough of how to &lt;em&gt;modify&lt;/em&gt; an array by adding and removing items. But there is another way which we can use to &lt;em&gt;access part of an array without modifying it&lt;/em&gt;. That concept is known as &lt;strong&gt;Array.slice()&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Array.slice() Method
&lt;/h3&gt;

&lt;p&gt;👉 Rather than modifying an array, &lt;code&gt;Array.slice()&lt;/code&gt; copies or extracts a given number of elements to a new array, leaving the &lt;em&gt;original array&lt;/em&gt; unmodified.&lt;br&gt;
👉 It returns a shallow copy of elements from the original array. Elements of the original array are copied into the returned array.&lt;br&gt;
👉 It can take only &lt;em&gt;two parameters&lt;/em&gt; and both of them are &lt;em&gt;optional&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The basic syntax are: &lt;br&gt;
&lt;code&gt;slice()&lt;/code&gt;&lt;br&gt;
or&lt;br&gt;
&lt;code&gt;slice(start)&lt;/code&gt;&lt;br&gt;
or&lt;br&gt;
&lt;code&gt;slice(start,end)&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Parameters of Array.slice()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 &lt;code&gt;start&lt;/code&gt; indicates the &lt;em&gt;index&lt;/em&gt; at which to &lt;em&gt;start extraction&lt;/em&gt;&lt;br&gt;
🔸 Index can be &lt;em&gt;negative&lt;/em&gt;, indicating an &lt;em&gt;offset&lt;/em&gt; from the &lt;em&gt;end&lt;/em&gt; of the sequence.&lt;br&gt;
🔸 The process generally starts extracting from index 0 if &lt;code&gt;start&lt;/code&gt; is undefined.&lt;br&gt;
🔸 If &lt;code&gt;start&lt;/code&gt; is greater than the index range of the sequence, an empty array is returned. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;end&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔸 &lt;code&gt;end&lt;/code&gt; indicates index &lt;em&gt;before which to end extraction&lt;/em&gt;.&lt;br&gt;
🔸 &lt;code&gt;slice()&lt;/code&gt; extracts &lt;em&gt;up to&lt;/em&gt; but not including &lt;em&gt;end&lt;/em&gt;. For &lt;em&gt;example&lt;/em&gt;, slice(1,4) starts extracting from index 1 through index 3. (elements indexed 1, 2, and 3 will be copied). &lt;br&gt;
🔸 A negative indexing can be used. For &lt;em&gt;example&lt;/em&gt;, slice(2,-1) extracts the third element through the second-to-last element in the sequence. &lt;br&gt;
🔸 &lt;code&gt;slice()&lt;/code&gt; extracts through the end of the sequence (array.length), if there is no &lt;code&gt;end&lt;/code&gt; specified.&lt;br&gt;
🔸 If &lt;code&gt;end&lt;/code&gt; is greater than the &lt;em&gt;length&lt;/em&gt; of the sequence, &lt;code&gt;slice()&lt;/code&gt; extracts through to the end of the sequence (array.length). &lt;/p&gt;

&lt;p&gt;A few examples will clear the concept more. &lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 1: Return a Copy of the Original Array
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;guitar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drums&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;violin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;piano&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tambourine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;extracted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;music&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'guitar', 'drums', 'violin', 'piano', 'tambourine', 'flute' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;extracted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'guitar', 'drums', 'violin', 'piano', 'tambourine', 'flute' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As &lt;em&gt;no parameter&lt;/em&gt; is passed within &lt;code&gt;slice()&lt;/code&gt;, it returns the whole array unchanged. &lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 2: Return a Part of the Existing Array, only using one parameter
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mango&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grape&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lemon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kiwi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;extracted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: ['mango', 'apple', 'orange', 'grape', 'lemon', 'banana', 'kiwi']&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;extracted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'grape', 'lemon', 'banana', 'kiwi' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the above &lt;em&gt;example&lt;/em&gt;, the &lt;em&gt;original array&lt;/em&gt; is unchanged after &lt;em&gt;slice()&lt;/em&gt; being called upon it but the new array &lt;em&gt;extracted&lt;/em&gt; holds the copies of the values starting from &lt;em&gt;index 3&lt;/em&gt;. As no &lt;code&gt;end&lt;/code&gt; is specified, &lt;code&gt;slice()&lt;/code&gt; extracted all the value through &lt;em&gt;array.length&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  🔹 Example 3: Return a Part of the Existing Array, using both parameters
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mango&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grape&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lemon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kiwi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;extracted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: ['mango', 'apple', 'orange', 'grape', 'lemon', 'banana', 'kiwi']&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;extracted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//output: [ 'orange', 'grape', 'lemon' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This &lt;em&gt;example&lt;/em&gt; shows, the &lt;em&gt;original array&lt;/em&gt; is unchanged after &lt;em&gt;slice()&lt;/em&gt; being called upon it but the new array &lt;em&gt;extracted&lt;/em&gt; holds the copies of the values starting from &lt;em&gt;index 2&lt;/em&gt; up to &lt;em&gt;index 5&lt;/em&gt; but not including the value of &lt;em&gt;index 5&lt;/em&gt;. The &lt;em&gt;extracted&lt;/em&gt; array holds the copies of values indexed at 2, 3 and 4.&lt;/p&gt;
&lt;h3&gt;
  
  
  Summarizing Array.splice() vs Array.slice()
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Array.splice()&lt;/th&gt;
&lt;th&gt;Array.slice()&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The &lt;code&gt;splice()&lt;/code&gt; method returns the removed item(s) in an array.&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;slice()&lt;/code&gt; method returns the selected element(s) in an array, as a new array object.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;This method &lt;em&gt;changes&lt;/em&gt; the original array.&lt;/td&gt;
&lt;td&gt;This method &lt;em&gt;doesn’t change&lt;/em&gt; the original array.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It &lt;em&gt;does not create any copy&lt;/em&gt; of the existing array, rather &lt;em&gt;modifies&lt;/em&gt; it.&lt;/td&gt;
&lt;td&gt;It just &lt;em&gt;creates a shallow copy&lt;/em&gt; of the existing array &lt;em&gt;without&lt;/em&gt; doing any &lt;em&gt;modification&lt;/em&gt; to it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can take &lt;em&gt;three&lt;/em&gt; parameters&lt;/td&gt;
&lt;td&gt;Can take &lt;em&gt;two&lt;/em&gt; parameters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When we write splice(2,4), it starts removing values from index 2 and remove 4 elements, &lt;em&gt;including&lt;/em&gt; the value of index 4 (removed items will be indexed at 2,3,4 &amp;amp; 5)&lt;/td&gt;
&lt;td&gt;When we write slice(2,4), it extracts values of index 2 up to index 4, &lt;em&gt;excluding&lt;/em&gt; the value of index 4 (removed items will be indexed at 2 &amp;amp; 3)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;In this method, the third parameter gives the &lt;em&gt;option to add&lt;/em&gt; one or more new items to the existing array&lt;/td&gt;
&lt;td&gt;There is no such &lt;em&gt;adding option&lt;/em&gt; in this method&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the end , I'll give only one problem to solve to my readers. Let's see how you solve this.&lt;/p&gt;

&lt;p&gt;🔲 Use &lt;code&gt;splice()&lt;/code&gt; to remove &lt;em&gt;"USA"&lt;/em&gt; first and then &lt;em&gt;from "Japan" to "Canada"&lt;/em&gt;. Insert &lt;em&gt;"Bhutan"&lt;/em&gt; in place of &lt;em&gt;"UK"&lt;/em&gt;. Use &lt;code&gt;slice()&lt;/code&gt; to keep a &lt;em&gt;copy&lt;/em&gt; of the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;countries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bangladesh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;India&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nepal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;USA&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UK&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Germany&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Japan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Spain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Canada&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ 'Bangladesh', 'India', 'Nepal', 'Bhutan', 'Germany' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Readers can post their solutions in the discussion part.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
