<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: itric</title>
    <description>The latest articles on DEV Community by itric (@itric).</description>
    <link>https://dev.to/itric</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%2F1121498%2F3a7ae1bc-4e67-4766-9a3d-7b22c0b568f6.png</url>
      <title>DEV Community: itric</title>
      <link>https://dev.to/itric</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itric"/>
    <language>en</language>
    <item>
      <title>Struggling with Large Codebases? Here’s the FAST-Track to Master Them!</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Sun, 01 Sep 2024 13:32:50 +0000</pubDate>
      <link>https://dev.to/itric/struggling-with-large-codebases-heres-the-fast-track-to-master-them-58kn</link>
      <guid>https://dev.to/itric/struggling-with-large-codebases-heres-the-fast-track-to-master-them-58kn</guid>
      <description>&lt;p&gt;Understanding a codebase is a complex process that involves learning how an existing software system works without prior knowledge of its inner workings. To help you with this problem, I have created 12 step framework to tackle any codebase with few advices :&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/bUlba_OVv6Q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;1) Set your objective:&lt;/p&gt;

&lt;p&gt;Determine why are you going to understand the codebase. Are you looking to understand its functionality, find bugs, improve performance, or gain insights for integration with other systems? This step is very beneficial as it helps you in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Knowing what you need to understand allows you to prioritize your time and effort, making the process more efficient.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Focused Learning&lt;/strong&gt;: Having a clear objective helps you concentrate on specific parts of the codebase relevant to your goals. This avoids getting overwhelmed by the entire codebase. Having a clear objective helps to focus on relevant parts of the codebase, avoiding unnecessary details that are not aligned with the goal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Goal Alignment&lt;/strong&gt;: Ensures that your understanding aligns with the specific goals of your project or task, whether it’s debugging, adding features, or refactoring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation Navigation&lt;/strong&gt;: Guides you in navigating through the documentation and other resources more effectively, focusing on the sections that matter most to your objective.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Questions&lt;/strong&gt;: With a clear objective, you can ask more targeted and relevant questions when seeking help online or with others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Progress Tracking&lt;/strong&gt;: Setting an objective allows you to measure your progress and determine whether you are on the right track towards understanding and achieving your goal.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;2) State the purpose of project (that codebase represents):&lt;/p&gt;

&lt;p&gt;State clearly and in-details, what that codebase is for, how it is being used, what it is serving. By doing so, this will help you in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Efficient Use of Time:&lt;/strong&gt; Knowing the purpose helps prioritize which sections of the code are most critical to review and understand, leading to a more efficient use of time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved Contextual Understanding:&lt;/strong&gt; It provides the necessary context to understand why certain decisions were made, such as design patterns, architecture choices, and specific implementations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Focused Analysis:&lt;/strong&gt; Understanding the purpose provides a clear direction, helping to focus on relevant parts of the codebase rather than getting lost in unnecessary details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, what to do when objective of codebase is not self-evidence or clear. To solve this problem; Gather any available documentation or resource, such as user manuals, design documents, API documentation, and comments within the code. These resources can provide valuable context and save time. Analyze code comments &lt;strong&gt;;&lt;/strong&gt; Pay close attention to comments within the code itself, as they often contain insights into the code's purpose and functionality. But even if codebase objective is clear you should gather information resources.&lt;/p&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;After completing these two steps, its time to get big picture overview of the codebase as it will help us to grasp the overall structure, design patterns, and key components. &lt;/p&gt;

&lt;p&gt;3) Explore project structure: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Understand the Directory Hierarchy:&lt;/strong&gt; Begin by examining the project's folder structure to get an overview of how the codebase is organized. Key directories to look for include are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;src&lt;/code&gt; (Source): Contains the main application code, including modules, classes, and functions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public&lt;/code&gt;: Holds public assets like HTML files, images, and other resources accessible directly via the web server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;components&lt;/code&gt;: Typically used in front-end projects to house reusable UI components.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tests&lt;/code&gt; or &lt;code&gt;specs&lt;/code&gt;: Contains unit tests, integration tests, and other testing-related code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;build&lt;/code&gt; or &lt;code&gt;dist&lt;/code&gt;: Holds the compiled or bundled output of the project, ready for deployment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs&lt;/code&gt;: Includes documentation related to the project, such as README files, API docs, and design documents.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Configuration files provide critical information about how the project is set up, built and run. Key configuration files to examine include package.json, webpack.config.js and .env :&lt;br&gt;&lt;br&gt;
    - &lt;code&gt;package.json&lt;/code&gt;: Found in Node.js projects, it lists project dependencies, scripts for common tasks (e.g., start, build, test), and metadata about the project.&lt;br&gt;&lt;br&gt;
    - &lt;code&gt;webpack.config.js&lt;/code&gt; or &lt;code&gt;rollup.config.js&lt;/code&gt;: Configuration for module bundlers, detailing how the project's files are compiled and packaged.&lt;br&gt;&lt;br&gt;
    - &lt;code&gt;.env&lt;/code&gt; or &lt;code&gt;.env.local&lt;/code&gt;: Environment variable configurations that store sensitive information and environment-specific settings.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;4) &lt;strong&gt;Identify the Entry Point:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Locate the Main Entry Point:&lt;/strong&gt; Find the primary starting point of the application where execution begins. This could be a &lt;code&gt;main()&lt;/code&gt; function in languages like C/C++, an &lt;code&gt;App.java&lt;/code&gt; file in Java, or an &lt;code&gt;index.js&lt;/code&gt; file in a Node.js project. Identifying the entry point provides a foundational understanding of how the application initializes and starts its operations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Understand Initialization Logic:&lt;/strong&gt; The entry point often contains critical initialization logic, such as setting up configurations, initializing dependencies, and starting services. By examining this code, you can gain insight into the application's initial setup and how different components are wired together.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Follow Execution Flow:&lt;/strong&gt; From the entry point, trace the flow of execution to understand how the program proceeds. Identify the key functions or methods that are called next, and follow the sequence of operations to get a sense of the application's structure and logic.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;5) &lt;strong&gt;Evaluate the Architecture&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Examine the Overall Architecture:&lt;/strong&gt; Understand the architectural style employed by the project, such as monolithic, microservices, layered, or event-driven. Determine how the application is organized into various modules, services, or layers and how these components interact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identify Architectural Patterns:&lt;/strong&gt; Look for common architectural patterns (e.g., MVC, MVVM, Singleton, Factory) and design principles used throughout the codebase. This can provide insights into the intended design and interactions within the codebase.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This holistic view will help you prioritize your efforts.&lt;/p&gt;

&lt;p&gt;6) Analyze dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check Package Managers&lt;/strong&gt;: If the project uses package managers (like npm for Node.js or Composer for PHP), review the dependency files to understand the libraries and frameworks used.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explore Third-Party Libraries&lt;/strong&gt;: Familiarize yourself with any third-party libraries included in the project. Check their documentation to understand how they are integrated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;APIs and Services:&lt;/strong&gt; Identify any third-party APIs or services the website integrates with and understand how they are used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Libraries and Frameworks:&lt;/strong&gt; Look at external libraries and frameworks and their roles in the project.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;7) Establish a hierarchy by &lt;strong&gt;Identify Critical (key) Components&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Core Functionality:&lt;/strong&gt; Pinpoint the core components that provide essential functionality for the application. These might include key modules, services, controllers, or classes that are central to the application's operation.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure Components:&lt;/strong&gt; Identify components responsible for infrastructure-related tasks such as database access, authentication, logging, and error handling.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Once you get the big picture of the codebase and have information about codebase’s structure, components’ hierarchy and codebase’s purpose, its time to utilize these knowledge to: &lt;/p&gt;

&lt;p&gt;8) Create user and programmer stories according to codebase:&lt;/p&gt;

&lt;p&gt;If it is a codebase of a software or app that which users can interact with, then create user stories.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create User Stories:&lt;/strong&gt;  take the time to document user stories that detail how users interact with the software. These stories should capture specific user actions, goals, and the pathways they follow within the application. By doing so, you not only clarify the purpose of various components but also illuminate their intended functionality within the broader user experience. This process helps you see the software from the user’s perspective, making it easier to understand the overall design and flow of the application. Furthermore, user stories can reveal the rationale behind certain design choices, aiding in the identification of key features and their interdependencies, ultimately making the codebase more approachable and navigable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If that’s not the case and it is some Legacy Codebases then, create programmer stories&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create Programmer Stories:&lt;/strong&gt;  document programmer stories that describe how the original developers constructed and organized the application. This involves understanding their thought processes, rationale behind architectural decisions, coding patterns, design choices they made and the evolution of the code over time. By tracing the thought process behind the code, you can gain insight into the developer's mindset and the challenges they faced, why certain approaches were taken, what challenges were addressed, which will help you comprehend the structure, purpose, and functionality of the code more effectively and how they fit into the broader architecture. This practice is invaluable for unraveling complex codebases and preserving the context behind key implementation choices.&lt;/p&gt;

&lt;p&gt;Both can be done in appropriate situation.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that you are done with this step, its to delve into details. Read the code thoroughly: Begin by examining the code line by line. Take detailed notes to build a mental model of its functionality and flow. Understanding the logic behind the code is essential for any modifications or enhancements you plan to make. Keep detailed notes of your observations, including the purpose of key functions, data structures, and algorithms. Diagrams can help visualize complex interactions.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Map the Application Flow:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Trace the Data Flow:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Follow the Data Path:&lt;/strong&gt; Examine the journey of data through the application from input to output. Identify key stages where data is received, processed, transformed, and eventually stored or presented.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Locate Data Processing Points:&lt;/strong&gt; Locate and understand critical points where data is manipulated, such as in functions, methods, or services that handle core logic and transformations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Track Variable Assignments and Function Calls:&lt;/strong&gt; Track how data is passed through different parts of the application by following variable assignments and function calls. This helps in understanding how data is manipulated and transferred across various components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pay Attention To Database Interactions:&lt;/strong&gt; Pay close attention to how the application interacts with databases. Understand how data is queried, inserted, updated, and deleted, and identify the key database operations and their role in the data flow.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Understand Control and Logic Flow:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Do Control Flow Analysis:&lt;/strong&gt; Analyze the flow of control through the application, focusing on major workflows and processes. Determine how different components and modules interact and the sequence of operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Map out User Interactions:&lt;/strong&gt; Map out how user actions and events are handled. Understand the flow from user input (e.g., button clicks, form submissions) to the corresponding application responses and updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nail down Business Logic:&lt;/strong&gt; Trace the core business logic that drives the application's functionality. Identify key functions and methods that encapsulate this logic, and understand how they interact with other parts of the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Examine Component Communication:&lt;/strong&gt; Examine how different components communicate with each other, including inter-process communication, API calls, and messaging between services. This helps in understanding the overall architecture and integration points.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Visualize the Flow:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Make Flow Diagrams:&lt;/strong&gt; Create visual representations such as flowcharts, sequence diagrams, or data flow diagrams to illustrate the flow of data and control within the application. These visual aids can help in quickly grasping complex flows and identifying potential bottlenecks or inefficiencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do Event Tracing:&lt;/strong&gt; Map out event flows to understand how events are propagated and handled throughout the system. This includes user events, system events, and asynchronous operations.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;At this point, you will have solid understanding of the codebase. Now, its time to test what you have learned. To do that:&lt;/p&gt;

&lt;p&gt;10) &lt;strong&gt;Set Up the Environment:&lt;/strong&gt;&lt;br&gt;
    - Ensure you have the necessary development environment to compile and run the code. This may involve setting up specific versions of programming languages, libraries, and tools.&lt;/p&gt;

&lt;p&gt;11) Test hypotheses, modify and experiment:&lt;br&gt;
    - &lt;strong&gt;Experiment with Small Changes:&lt;/strong&gt;&lt;br&gt;
        - &lt;strong&gt;Validate Your Understanding:&lt;/strong&gt; Make minor modifications to the codebase to test your understanding of its behavior. For instance, change a variable value, adjust a condition in an if-statement, or modify a function's implementation. Observe how these changes impact the overall application to confirm or refine your comprehension.&lt;br&gt;
        - &lt;strong&gt;Analyze Effects:&lt;/strong&gt; Carefully analyze the effects of your changes on the application's functionality. Pay attention to both expected and unexpected outcomes, as they can provide valuable insights into how different parts of the codebase are interconnected.&lt;br&gt;
    - &lt;strong&gt;Hands-on Enhancement and Fixes:&lt;/strong&gt;&lt;br&gt;
        - &lt;strong&gt;Implement Small Enhancements:&lt;/strong&gt; If your objective is to improve the code, start with small, incremental changes. This could involve optimizing a function, refactoring a piece of code, or enhancing a feature. Gradually build on these changes as you gain confidence in your understanding.&lt;br&gt;
    - &lt;strong&gt;Utilize Version Control:&lt;/strong&gt;&lt;br&gt;
        - &lt;strong&gt;Track Changes with Git:&lt;/strong&gt; Use Git or another version control system to track your modifications. Commit your changes frequently with descriptive messages, detailing what was changed and why. This practice helps in maintaining a clear history of your work.&lt;br&gt;
        - &lt;strong&gt;Create Branches for experiment:&lt;/strong&gt; Work on separate branches for different experiments or enhancements. This allows you to isolate your changes and test them independently without affecting the main codebase.&lt;br&gt;
        - &lt;strong&gt;Revert Changes When Necessary:&lt;/strong&gt; If a change leads to unintended consequences or breaks the application, use Git to revert to a previous stable state. This safety net encourages experimentation without the fear of causing irreversible damage.&lt;/p&gt;

&lt;p&gt;12) Review similar codebases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Study Open-Source Projects:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identify Relevant Projects:&lt;/strong&gt; Search for open-source projects that are similar in scope, technology stack, or functionality to the codebase you are working on. Platforms like GitHub, GitLab, and Bitbucket are excellent resources for finding such projects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analyze Common Patterns:&lt;/strong&gt; Examine these projects to identify common architectural patterns, coding conventions, and best practices. Look for how these projects structure their code, manage dependencies, handle data flow, and implement core functionalities.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Understand Problem-Solving Approaches: (not)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Solution Strategies:&lt;/strong&gt; Observe how similar problems are addressed in other projects. Compare different approaches to solving the same problem to understand the trade-offs and benefits of each method.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization Techniques:&lt;/strong&gt; Pay attention to optimization techniques used in other codebases, such as performance improvements, resource management, and scalability enhancements.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Aside from these steps, it is also important to:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Seek Help and Resources&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you encounter challenges, consider reaching out to online forums or communities (like Stack Overflow) and LLMs for assistance. Utilize official documentation for any frameworks or libraries used in the project to clarify functionality and best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Last advice:
&lt;/h2&gt;

&lt;p&gt;But even if you follow all these step, there is high chance that your retention of all these information will be low. And how we even properly synthesis and organize all of this information without getting overwhelmed. To do that I recommend to make mind-map. &lt;/p&gt;

&lt;p&gt;Group all the modules, components, or code blocks based on their functionality, behavior, and implementation. Consider how all these groups relate to each other, such as through cause-and-effect relationships, chronological sequences or conceptual links. Express these relationships clearly in your mind map.&lt;/p&gt;

&lt;p&gt;Use symbols instead of words to represent different elements and ensure the mind map has a clear direction or flow. Utilize arrows to demonstrate how the ideas in your mind map interact with each other. Emphasize the most important parts of the mind map by indicating clear and deliberate judgments about their significance. This will help in visually distinguishing the priority and relevance of various components within the codebase.&lt;/p&gt;

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

</description>
      <category>tutorial</category>
      <category>learning</category>
      <category>softwaredevelopment</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>🤔 How e.target and e.currentTarget is used for Modal in React app ?</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Mon, 12 Aug 2024 03:41:22 +0000</pubDate>
      <link>https://dev.to/itric/how-etarget-and-ecurrenttarget-is-used-in-modal-in-react-app-4occ</link>
      <guid>https://dev.to/itric/how-etarget-and-ecurrenttarget-is-used-in-modal-in-react-app-4occ</guid>
      <description>&lt;p&gt;In a React application, &lt;code&gt;e.target&lt;/code&gt; and &lt;code&gt;e.currentTarget&lt;/code&gt; can be used to add closing functionality to a modal when clicked outside of the modal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's how:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/3kCgeI7KUdY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isModalOpen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsModalOpen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;" bg-white h-screen w-screen flex flex-col items-center justify-center"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
          &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setIsModalOpen&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="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"w-full max-w-xs font-bold hover:opacity-70 relative text-white bg-[#635fc7] py-2 rounded-full"&lt;/span&gt;
        &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          Open Modal
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isModalOpen&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-md"&lt;/span&gt;
          &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// Click inside the modal; don't close it&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nf"&gt;setIsModalOpen&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="c1"&gt;// Click outside the modal; close it&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"max-w-sm relative bg-white rounded-lg p-6"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Modal&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
              &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"absolute top-2 right-2 text-gray-600 hover:text-gray-900"&lt;/span&gt;
              &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setIsModalOpen&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="si"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="ni"&gt;&amp;amp;times;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-2xl mb-4"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Modal Content&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is a sample modal. Click the &lt;span class="ni"&gt;&amp;amp;times;&lt;/span&gt; to close it. Or Click anywhere outside of the modal &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation :
&lt;/h2&gt;

&lt;h2&gt;
  
  
  State Management:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A state variable, &lt;code&gt;isModalOpen&lt;/code&gt;, is used to manage the visibility of the modal. It's initially set to false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A function, &lt;code&gt;setIsModalOpen&lt;/code&gt;, updates the value of &lt;code&gt;isModalOpen.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isModalOpen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsModalOpen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conditional Rendering:
&lt;/h2&gt;

&lt;p&gt;The modal is conditionally rendered based on the value of &lt;code&gt;isModalOpen&lt;/code&gt;. If true, the modal is displayed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isModalOpen&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Modal&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Opening and Closing the Modal:
&lt;/h2&gt;

&lt;p&gt;The "Open Modal" button triggers &lt;code&gt;setIsModalOpen(true)&lt;/code&gt;, which sets &lt;code&gt;isModalOpen&lt;/code&gt; to true, rendering the modal.&lt;/p&gt;

&lt;p&gt;The modal can be closed by clicking the "×" ("Close Modal") button inside it, which triggers &lt;code&gt;setIsModalOpen(false)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setIsModalOpen&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Open Modal&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isModalOpen&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setIsModalOpen&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Close Modal&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modal Closing Logic:
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;onClick&lt;/code&gt; event is attached to the outermost &lt;code&gt;div&lt;/code&gt; that acts as the modal's background or overlay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inside the event handler:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;e.target&lt;/code&gt; refers to the actual element that was clicked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;e.currentTarget&lt;/code&gt; refers to the element that the event listener is attached to (in this case, the outer &lt;code&gt;div&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To close the modal when a click occurs outside of it, the following logic is implemented:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Propagation Check:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;onClick&lt;/code&gt;handler of the outer &lt;code&gt;div&lt;/code&gt;, a check is performed: if (&lt;code&gt;e.target !== e.currentTarget&lt;/code&gt;). This condition verifies whether the click happened on the outer &lt;code&gt;div&lt;/code&gt; itself (&lt;code&gt;e.currentTarget)&lt;/code&gt; or on any of its child elements (like the modal content).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditional Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;e.target&lt;/code&gt;is not equal to &lt;code&gt;e.currentTarget&lt;/code&gt;, it means the click occurred inside the modal (on one of its child elements). In this case, the function returns early, preventing the modal from closing.&lt;/p&gt;

&lt;p&gt;If the click was on the outer &lt;code&gt;div&lt;/code&gt; (i.e., &lt;code&gt;e.target === e.currentTarget)&lt;/code&gt;, the modal is closed by setting &lt;code&gt;isModalOpen&lt;/code&gt;to false.&lt;br&gt;
This approach ensures that the modal only closes when the user clicks outside of it, while clicks inside the modal (on its content or buttons) do not trigger the close action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; 
          &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTarget&lt;/span&gt;&lt;span class="p"&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="c1"&gt;// Click inside the modal; don't close it&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nf"&gt;setIsModalOpen&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="c1"&gt;// Click outside the modal; close it&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Points:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;State Management: The visibility of the modal is controlled by a boolean state (&lt;code&gt;isModalOpen&lt;/code&gt;), which is toggled based on user interaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event Handling: The combination of &lt;code&gt;e.target&lt;/code&gt; and &lt;code&gt;e.currentTarget&lt;/code&gt;allows us to differentiate between clicks inside and outside the modal, ensuring that the modal only closes when a click occurs outside its content.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>THE Ultimate Guide to DRY RUNS: Build STRONGER Logic 💪!</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Sun, 11 Aug 2024 08:49:00 +0000</pubDate>
      <link>https://dev.to/itric/the-ultimate-guide-to-dry-runs-build-stronger-logic--54f4</link>
      <guid>https://dev.to/itric/the-ultimate-guide-to-dry-runs-build-stronger-logic--54f4</guid>
      <description>&lt;p&gt;Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously.&lt;/p&gt;

&lt;p&gt;This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed.&lt;/p&gt;

&lt;p&gt;To be able to do this, one essential step that many beginner programmers overlook is performing dry runs.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/oIkdb37S9kg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Dry Runs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building strong logic skills.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpw7cp1xz6hzz2jhl90e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzpw7cp1xz6hzz2jhl90e.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Challenge of Dry Runs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The process of dry running involves visualizing the code execution in your mind. For example, when dealing with loops, you should be able to mentally trace the iterations and understand the changes in variables at each step. This ability is critical, especially for complex logic and pattern printing tasks.&lt;/p&gt;

&lt;p&gt;Often, beginners face difficulties with pattern printing and matrix operations because they fail to visualize the flow of the loops. They might write code with nested loops but struggle to understand how these loops interact to produce the desired output. This lack of visualization hinders their ability to debug and optimize code effectively.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1c8jwqbmdv07wcc561kd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1c8jwqbmdv07wcc561kd.png" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Understand data flow
&lt;/h3&gt;

&lt;p&gt;The most crucial aspect of getting better at dry running the code is understanding data flow.&lt;/p&gt;

&lt;p&gt;Many people grasp the concept of loops—how they iterate and repeat processes. However, the true challenge lies in understanding how data flows through each step of the loop. How does data get processed and transformed at each stage? This is not straightforward. Dry running your code on paper is essential. This means taking a pen and paper and manually tracing the value of your data at each step. By doing so, you can visualize and understand the transitions and transformations your data undergoes, making it easier to identify logical errors and inefficiencies.&lt;/p&gt;

&lt;p&gt;This understanding becomes even more critical when dealing with advanced methods in programming languages like JavaScript. Functions such as map, filter, and reduce add layers of complexity to data manipulation. Understanding how data flows, iterates, and changes during the execution of these methods is vital. &lt;/p&gt;

&lt;p&gt;Mastering data flow is your foundational step. Before diving into writing complex logic, take the time to meticulously analyze your data flow. Break down each step and understand how data moves and changes throughout your program. This disciplined approach not only helps in debugging but also in optimizing your code.&lt;/p&gt;

&lt;p&gt;Furthermore, this practice significantly enhances your logic-building skills. By understanding the intricacies of data flow, you can create more efficient algorithms and solve problems more effectively. Therefore, make it a habit to dry run your code and focus on the data flow. This will lay a solid foundation for your programming journey and lead to greater success in tackling more advanced coding challenges.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Dependence on Execution is Problematic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are constantly dependent on running your code to understand its behavior, it indicates a weakness in your logical thinking. As a coder, you should aim to predict the output before executing the code. This skill is vital because, in large-scale applications, immediate execution and testing might not be feasible due to dependencies and complex interactions between different parts of the codebase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F23ug0ieks22z1ch1ulfi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F23ug0ieks22z1ch1ulfi.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In real-world scenarios, you often cannot test every functionality immediately after writing it. Dependencies on other components, code from colleagues, or external systems mean that your code might not run as expected until the entire system is integrated. Therefore, being able to mentally simulate code execution is invaluable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Improve Dry Running Skills&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Practice Pattern Printing:&lt;/strong&gt; Start with simple pattern printing exercises. Try to predict the output without running the code. Focus on understanding the role of each loop and how it affects the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Break Down the Code:&lt;/strong&gt; Analyze the code line by line and understand what each line does. For loops, identify which loop controls the rows and which controls the columns. Visualize the iteration process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write Test Cases:&lt;/strong&gt; Before running the code, write down test cases and predict the output. After running the code, compare the actual output with your predictions. This practice will help you understand where your logic might be flawed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Pen and Paper:&lt;/strong&gt; For complex logic, use pen and paper to trace the flow of data and control structures. This manual process helps reinforce your understanding of the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Work on Debugging:&lt;/strong&gt; Try debugging without running the code. Identify potential issues by examining the logic and flow. This practice enhances your ability to spot errors and optimize code efficiently.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Long-term Benefits of Dry Runs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developing the skill to execute code mentally prepares you for larger projects and complex applications. It builds your confidence in your logical thinking and problem-solving abilities. Over time, this skill will enable you to write more efficient, error-free code and improve your overall coding proficiency.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkvwvkzk843va7n28l37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkvwvkzk843va7n28l37.png" alt="Image description" width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, while executing code to see the output is necessary, relying solely on this method limits your growth as a programmer. Embrace the challenge of dry running your code. It will enhance your logic-building skills and make you a more proficient and confident coder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To think like a programmer, it requires aligning your thought process with the computer's inner working and abstractly running through the code to predict its behavior upon execution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac2bmg97bn5s1mzbofyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac2bmg97bn5s1mzbofyt.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>coding</category>
      <category>programming</category>
      <category>dsa</category>
    </item>
    <item>
      <title>How to ACTUALLY learn from Coding Tutorials (Step-by-Step Guide) || part three.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Mon, 22 Jul 2024 05:21:41 +0000</pubDate>
      <link>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-three-6il</link>
      <guid>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-three-6il</guid>
      <description>&lt;p&gt;(If you prefer watching video: &lt;a href="https://www.youtube.com/watch?v=XW9-7wn6KoA" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=XW9-7wn6KoA&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;So we've prepared, we've gone through the tutorial, and now it’s time to review what you have learned. At this stage, you’ll be revisiting the same material for the third time. &lt;/p&gt;

&lt;p&gt;And in this article, we will be going through some useful strategies and techniques to do so.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effective Review Techniques:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Recreate the Project:&lt;/strong&gt; Write down the steps needed to recreate the project from scratch. Or try to build it again without any help from tutorial or if it’s too hard, then you can cheat here and there, but mostly from you own knowledge. This will help you to internalize the process. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhance the Project:&lt;/strong&gt; Try adding extra functionality to your codebase or app. This could involve implementing new features or optimizing existing ones, which will deepen your understanding and give you practical experience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mind-Mapping:&lt;/strong&gt; Create a mind map of the concepts covered in the tutorial. The important thing here is to create groups of similar concepts, denote relation between them with lines, arrows and make sure to indicate the flow of information or steps and concepts through arrows. This visual representation helps you see how different ideas are connected and aids in memory retention.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Document Your Work:&lt;/strong&gt; Write detailed documentation for the project you’ve built. This practice not only reinforces what you’ve learned but also creates a valuable reference for future projects. And to help you in this process; &lt;/p&gt;

&lt;p&gt;Here are some questions you should ask and answer to properly review the project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Project Overview:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What is the purpose of the project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "The project is a basic to-do list application aimed at helping users manage their daily tasks."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What technologies were used?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "The project was built using HTML, CSS, and JavaScript for the front end, with Node.js and Express for the back end, and MongoDB as the database."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core Functionality:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What are the main features of the project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "Users can add, delete, and mark tasks as complete. Tasks are stored in a MongoDB database and persist across sessions."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How does each feature work?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "When a user adds a task, it sends a POST request to the server, which saves the task in the database and updates the front-end display."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Analysis:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How is the code structured?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "The project is organized into separate folders for routes, models, and views. The server.js file handles the main server logic."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What are the key functions or classes, and what do they do?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "The &lt;code&gt;addTask&lt;/code&gt; function handles creating new tasks, the &lt;code&gt;deleteTask&lt;/code&gt; function handles removing tasks, and the &lt;code&gt;markComplete&lt;/code&gt; function updates task status."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How do different parts of the code interact?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "The front-end JavaScript sends AJAX requests to the Express routes, which then interact with the MongoDB database through Mongoose models."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Challenges and Solutions:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What challenges did you face while building the project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I had difficulty implementing the update functionality for tasks due to unfamiliarity with MongoDB update operations."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How did you overcome these challenges?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I consulted the Mongoose documentation and experimented with different update methods until I found the correct approach."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning Outcomes:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What new concepts or skills did you learn from this project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I learned how to set up a basic Express server, connect it to a MongoDB database, and use Mongoose for data modeling."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How did this project improve your coding abilities?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "It improved my understanding of RESTful APIs and enhanced my ability to debug JavaScript code."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Potential Improvements:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What could be improved or added to the project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I could add user authentication to allow multiple users to manage their own task lists and improve the front-end design for better user experience."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How would you implement these improvements?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I would use Passport.js for authentication and redesign the interface using a front-end framework like Bootstrap."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reflection:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What did you enjoy most about this project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I enjoyed seeing the project come together and function as intended, especially when the database interactions worked smoothly."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What was the most challenging part of the project?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "Debugging the asynchronous code in JavaScript was the most challenging part because it required careful attention to how promises and callbacks were handled."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What would you do differently if you started the project again?&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Example: "I would spend more time planning the project structure and design before jumping into coding to avoid major refactoring later on."&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Free Recall:&lt;/strong&gt; One of the simplest review technique is free recall. Just take out a blank sheet of paper and write down everything you can remember about the tutorial. Organize and synthesize the information, and then use resources like ChatGPT, documentation, or the internet to fill in any gaps.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Another question you may ask is, how much time should we wait to do review after finishing tutorial?&lt;/p&gt;

&lt;p&gt;So ideally, you should review the material relatively soon after completing the tutorial. It doesn’t have to be immediately, but don’t wait longer than a day or so. Even if you only perform this free recall exercise once a week, it will be immensely helpful, especially if you’re not doing any other kind of reflection or synthesis of your learning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Discuss and Collaborate:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;With Fellow Programmers:&lt;/strong&gt; Discussing the material with fellow programmers can provide new perspectives and insights. Collaboration often leads to a deeper understanding.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;With Non-Programmers:&lt;/strong&gt; Explaining the concepts to someone without a programming background can also be incredibly beneficial. It forces you to bring more background knowledge and context around the things, that you are trying to convey. And they may ask questions that you hadn’t even considered yet !.&lt;/li&gt;
&lt;li&gt;And if you don’t find or have anyone, you can imagine you are explaining it to someone. Verbalizing and articulating things always helps when it comes to build higher understanding of the things.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Consolidate Your Knowledge:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The key is to actively engage in some form of review to solidify your understanding. Sometimes this review process will blend into preparation for the next concept or project you will be working on. Many projects have overlapping concepts and steps. For example, if you are working on three projects all related to e-commerce apps or web scraping, reviewing the first project will help prepare you for the next ones as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't Neglect the Fundamentals:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Avoid skipping over topics you think you already know. It’s easy to assume familiarity means understanding, but often, fundamentals need to be revisited and deeply understood. Most subjects I've learned in life became clearer much later on, when I realized the importance of those basic concepts and I actually had a very superficial understanding of those things. I always find myself going back to the foundational principles because they underpin everything else. And I am sure many of you had similar experience of sort.&lt;/p&gt;

&lt;p&gt;Understanding the fundamentals, well makes advanced topics more intuitive and easier to grasp. There’s a reason why so many experts emphasize on those basics  – they are the building blocks for everything that follows. If you have a solid grasp of the fundamentals, other topics become extensions of that core knowledge, making much more sense.&lt;/p&gt;




&lt;p&gt;In a lot of cases when you are using a certain framework, library, programming tool, or even a programming language itself, it's crucial to understand the core principles and the specific cases where these principles play a significant part. These core principles form the foundation, and most techniques, tricks, and steps are derived from these fundamental rules.&lt;/p&gt;

&lt;p&gt;In any complex project, there are numerous small components. These little bits work together within larger systems to accomplish specific processes and create the desired behavior for the final project or product. While it's important to understand these individual components, it's equally vital to comprehend how they integrate into larger systems. The more you understand how these systems collaborate to achieve certain goals, the more sense the individual components will make.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On the importance and process of review:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reviewing what you’ve learned helps you see the bigger picture and clarifies how everything fits together. One thing you might want to do is to link this project to our previous project that you have made. This connection helps reinforce your understanding and shows how different concepts and techniques can be applied across various projects.&lt;/p&gt;

&lt;p&gt;Keep in mind, these are just some basic ideas to guide your study sessions. You must exercise and use your own judgment and apply these strategies wisely. There isn't a single correct way to learn from tutorials. The best approach depends on several factors, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What You're Learning:&lt;/strong&gt; Different topics might require different study techniques.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your Preferences:&lt;/strong&gt; Some people learn better through hands-on practice, while others might prefer reading or watching videos and try to connect things in their head while doing that. And some may just want to draw out things.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your Prior Knowledge:&lt;/strong&gt; Your existing understanding of the subject can influence how you see and approach new material. As it will be easier for you to build up on those things.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Available Resources:&lt;/strong&gt; The resources you have access to, such as friends, courses, source codes, and online communities are can also play a huge role.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And don’t worry, you don’t have to follow these steps every single time. Even incorporating these strategies occasionally can significantly boost your learning experience.&lt;/p&gt;

&lt;p&gt;By understanding the core principles, recognizing the role of individual components within larger systems, and wisely applying review strategies, and by also using techniques and strategies discussed in previous articles you can properly utilize the most of  the coding tutorial video that you watch.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to ACTUALLY learn from Coding Tutorials (Step-by-Step Guide) || part two.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Tue, 16 Jul 2024 02:49:00 +0000</pubDate>
      <link>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-two-4n1f</link>
      <guid>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-two-4n1f</guid>
      <description>&lt;p&gt;(If you prefer to watch a video : &lt;a href="https://www.youtube.com/watch?v=OUqG7s0c33U&amp;amp;t=36s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=OUqG7s0c33U&amp;amp;t=36s&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;So now  that you have some ideas about how to prepare before a tutorial. But what should we be doing during the tutorial itself?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Engage Actively and Pay Attention:&lt;/strong&gt; The main goal during the tutorial is to stay engaged and pay close attention. However, paying attention is more than just watching passively and just following along. It means interacting with the material in a way that helps you make sense of what’s being taught.&lt;/p&gt;

&lt;p&gt;For example, if drawing diagrams or taking notes helps you focus, then do so, but aim to synthesize the material rather than just transcribing everything verbatim. Summarize key points, jot down important concepts, and make note of any questions that arise as you follow along. Try to simplify and evaluate steps and concepts used, try to relate steps and concepts. Making mind-map can also be great help for this purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observe Closely:&lt;/strong&gt; While observing closely is important, it's crucial to know what to look for. This can depend on the type of tutorial and your existing knowledge. Here are some things to focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Connections Between Steps:&lt;/strong&gt; Pay attention to the links between the steps or the ideas that instructor in the video is talking about. Understanding these links can help you see the bigger picture and understand the logic behind the process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Question the Approach:&lt;/strong&gt; Ask yourself questions like, “Why is the problem solved this way and not another way?”, “Why this feature is implemented with these sequence of steps?” or “Could we solve this problem differently?” This critical thinking helps deepen your understanding and helps  you to be actively engaged. You can think yourself as detective trying to solve a case, look for connection between evidences (in our context, what instructor is saying, code and documentations). LLMs can also be great help for this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare and Contrast:&lt;/strong&gt; Look for similarities and differences in the code or concepts presented. For instance, what makes one piece of code work or behave different from another? Or, how are two seemingly different approaches fundamentally the same? This analytical approach can boost your comprehension and retention.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Identify Inconsistencies:&lt;/strong&gt; If you notice any inconsistencies or tensions between what the instructor says and what you already know, that’s a good sign you're paying attention in the right way. This is another area where preparation helps. If you have some background knowledge, it’s easier to spot these discrepancies and understand the material more deeply and it will be more interesting and relevant to you. Not just a tsunami of new information.&lt;/p&gt;

&lt;p&gt;By staying engaged, asking questions, and critically analyzing the material, you make the most out of the tutorial. This active engagement not only helps you grasp the content better but also makes the learning process more enjoyable and effective.&lt;/p&gt;

&lt;p&gt;But it takes a lot of effort to maintain focus throughout a tutorial in this manner. Typically, after 10 to 20 minutes without some kind of change in format or engaging activity, attention tends to drop significantly.&lt;/p&gt;

&lt;p&gt;However, online tutorial videos offer a significant advantage: you have complete control over the pace and flow of the information. You can stop the video at any time, rewind to review different parts, speed it up if it's moving too slowly, or slow it down for complex sections. This flexibility allows you to tailor the learning experience to your needs.&lt;/p&gt;

&lt;p&gt;Many of us, what we do is, we try binge watch the whole tutorial. Well, it can give us a false sense that we are learning. But its all shallow and we will forget all the content in a day or two, if not within hours.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Pause and Reflect:&lt;/strong&gt; I recommend stopping the video frequently, especially when the instructor is about to implement a new feature or explain what they are going to do next or presents a problem. At these points, pause the video and take a moment to think about how you would implement the feature or write the code. Even if you don't know the exact solution, try to understand and break down the problem before the instructor explains it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Ask Critical Questions:&lt;/strong&gt; To deepen your understanding, ask yourself questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"What kind of conditional statements can be used?"&lt;/li&gt;
&lt;li&gt;"Do I need to use any third-party packages for this?"&lt;/li&gt;
&lt;li&gt;"What kind of data entities and data types are applicable here?"&lt;/li&gt;
&lt;li&gt;"What potential errors or exceptions should I anticipate and handle?"&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How does this piece of code fit into the overall architecture of the project?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"Are there any security considerations I need to be aware of?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How would I debug this code if something goes wrong?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"Are there alternative methods to achieve the same result? What are their pros and cons?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How can I test this code to ensure it works correctly?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"What are the dependencies for this code to run properly?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How would I document this code for future reference or for other developers?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How does this concept or technique relate to others I've learned?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"What design patterns could be applied here?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"How would I refactor this code to improve its structure?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"What are the possible impacts on other parts of the project if I change this code?"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"What are the trade-offs involved in using this approach?"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;These questions help you think critically about the tutorial content, encouraging a deeper understanding and a more holistic view of the coding concepts being taught.&lt;/p&gt;

&lt;p&gt;And help you engage actively with the material and develop a problem-solving mindset.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Regular Breaks:&lt;/strong&gt; After every 10 to 15 minutes, pause the video to collect your thoughts and review what you've learned. Reflect on the current progress and anticipate what might come next. This helps reinforce your understanding and keeps you focused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limit Continuous Viewing:&lt;/strong&gt; If you find yourself watching a tutorial for more than 30 minutes without a break, it's a sign that you may not be getting the most out of it. Long, uninterrupted viewing sessions can lead to cognitive overload and reduce the effectiveness of your learning. By taking regular breaks and actively engaging with the material, you ensure that you are absorbing and retaining the information more effectively.&lt;/p&gt;

&lt;p&gt;By following these strategies, you can maintain a high level of attention and maximize the benefits of online tutorials. Active engagement and regular breaks will help you better understand and retain the material, making your learning experience more productive and enjoyable.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>tutorial</category>
      <category>coding</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to ACTUALLY learn from Coding Tutorials (Step-by-Step Guide) || part one.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Thu, 11 Jul 2024 10:37:00 +0000</pubDate>
      <link>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-one-339f</link>
      <guid>https://dev.to/itric/how-to-actually-learn-from-coding-tutorials-step-by-step-guide-part-one-339f</guid>
      <description>&lt;p&gt;(If you prefer watching video: &lt;a href="https://www.youtube.com/watch?v=8xCrkuGrCT8&amp;amp;t=57s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=8xCrkuGrCT8&amp;amp;t=57s&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Coding tutorials are invaluable resources for anyone aiming to learn to code or deepen their understanding of new and important programming concepts. Whether you're a beginner or an experienced developer, these tutorials can provide step-by-step guidance and practical insights to enhance your skills.&lt;br&gt;
In this article, I'll discuss strategies to maximize the benefits of any coding tutorial you watch. By breaking down the learning process into three distinct phases—before, during, and after watching a tutorial—you can optimize your learning experience and make the most of the content.&lt;/p&gt;

&lt;p&gt;Each phase plays a crucial role in reinforcing your knowledge and ensuring you gain practical, applicable skills. So, let's dive in and explore how to effectively engage with coding tutorials to boost your programming prowess.&lt;/p&gt;

&lt;p&gt;Let's start with preparation. It's almost never a good plan to jump right into a tutorial without any groundwork.&lt;br&gt;
Most of us, what we do is we jump right into tutorial and start to follow along without any second thought. However, doing some groundwork preparations can make a big difference. &lt;/p&gt;

&lt;p&gt;Additionally, Before starting to watch a tutorial, check if the source code is provided. If not, you might want to skip that tutorial. It's not that you can't learn from it, but it would be much harder since you won't be able to cross-check if you encounter problems while building a project.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;But how do you prepare for a coding tutorial? Here are some steps you can take:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Scan Through the Source Code and tutorial video:&lt;/strong&gt; Before you start the tutorial, take some time to scan through the provided source code. Get a general idea of how the files are organized and how the components or sections of the code are connected. This will give you a broad overview of the project structure and help you follow along more easily during the tutorial. You can also scan through the tutorial video by jumping through time frames or speeding up the video. This quick overview will help you understand the flow and main points of the tutorial. It's okay if you run into trouble or don't quite understand everything right away. Learning something new on your own can be challenging, but the important part is to get some experience and lay a foundation. You're planting seeds of knowledge, and the tutorial will help water those seeds, allowing them to grow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identify Unfamiliar Code:&lt;/strong&gt; As you review the source code, identify any lines or blocks of code that you don’t understand. Highlight these sections and make a note of them. This will help you focus your attention on areas where you need the most clarification. Along that way, make probable assumptions and draw out what possible final product might look like. It’s okay if you get it wrong, part of a learning process is to get it wrong first then correcting it or learning from it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make a List of Questions and Identify Gaps:&lt;/strong&gt; Based on your initial scan, compile a list of questions about the code and concepts you don’t fully grasp or areas where you're confused. These questions will be invaluable as you watch the tutorial, allowing you to seek out specific answers and explanations as they arise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Research and Prepare:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s say you find out that there is a concept of "asynchronous programming in JavaScript," used in tutorial or you find keywords like async, await, promise and try-catch in source code. And your understanding of it is a bit rusty. What you can do is, to prepare effectively, start by researching the basic concepts involved. For instance, look up explanations and examples of asynchronous programming, focusing on key techniques like callbacks, promises, and async/await. There are numerous online resources, including articles and videos, that can help you understand these concepts beforehand.&lt;/p&gt;

&lt;p&gt;Work through a few example problems where these techniques are applied. You might try writing basic asynchronous functions yourself, such as fetching data from an API and handling it with promises. This hands-on practice will give you a foundational understanding, making the tutorial much easier to follow and comprehend.&lt;/p&gt;

&lt;p&gt;This process can be applied for any complex topic and not just in programming.&lt;/p&gt;

&lt;p&gt;By taking these preparatory steps, you’ll be better equipped to understand and retain the information presented in the tutorial. Preparation not only helps you follow along more effectively but also enhances your overall learning experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reduce Cognitive Load:&lt;/strong&gt; One of the main goals of preparation is to reduce cognitive load—the amount of mental effort required to process new information. By familiarizing yourself with basic concepts beforehand, you free up mental resources to understand more complex ideas during the tutorial.&lt;/p&gt;




&lt;p&gt;During this process, you should also ask questions like "Is this tutorial right for my current knowledge and skill level?" and "Is it a next logical step for my learning?"&lt;/p&gt;

&lt;p&gt;It's important to ensure that the tutorial matches your current abilities and learning goals. Sometimes, you might find that the tutorial is either too challenging or too basic for you. If the tutorial is too advanced, you might struggle to keep up and miss out on key concepts. On the other hand, if it covers material you already know, you won’t gain much from it, making it a less efficient use of your time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Assess the Tutorial's Difficulty:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Too Challenging:&lt;/strong&gt; If the tutorial seems too difficult, consider what are you missing. Look for beginner or intermediate-level tutorials that build up to the advanced concepts you're aiming to learn.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Too Basic:&lt;/strong&gt; If the tutorial covers concepts you already understand, it might be better to skip it and find more advanced resources. Continuously challenging yourself with new and complex material ensures steady progress in your learning journey.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optimize Your Learning Path:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Select the Right Tutorial:&lt;/strong&gt; Choosing the appropriate tutorial for your current skill level is crucial. A well-suited tutorial should stretch your abilities without overwhelming you. It should introduce new concepts that build on your existing knowledge. Yeah, it hard to find these tutorial, so don’t be strict with this rule. Don’t waste too much time in hunting video tutorials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adapt as Needed:&lt;/strong&gt; Be flexible in your learning approach. If you start a tutorial and realize it’s not the right fit, don’t hesitate to switch to another resource. Your time is valuable, and focusing on the most beneficial content is key to effective learning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By carefully selecting tutorials that align with your skill level and learning goals, you maximize the efficiency and effectiveness of your study sessions. This strategic approach helps ensure that each tutorial you follow provides you with valuable knowledge and skill development.&lt;/p&gt;

&lt;p&gt;So back to my point, during the tutorial; you might be bombarded with new words, new concepts, new ways of doing things and new techniques. As that happens, our working memory has to deal with too much stuff at once. And during that, what happens is, stuff just starts to Fall Away. We end up not really grasping much of anything. That’s why, certain amount of work before watching tutorial is valuable.&lt;/p&gt;




&lt;p&gt;Another important goal is to form a cohesive understanding of the material by integrating information that you acquire. So you start by picking up a few key concepts before the tutorial. As you follow along, you'll build on what you’ve already learned, connecting new ideas with what you know. Even during the review, you will pick up a few more things which will extend what you learned from the tutorial further.&lt;/p&gt;

&lt;p&gt;If you view learning as a developmental process rather than just a transmission of information, it becomes clear that deep understanding requires engaging with the material multiple times in various ways to get a really deep understanding of the material.&lt;/p&gt;

&lt;p&gt;This integrated understanding helps you retain knowledge more effectively and apply it more confidently in real-world situations.&lt;br&gt;
Happy coding !&lt;/p&gt;

</description>
      <category>learning</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>coding</category>
    </item>
    <item>
      <title>STRUGGLING with React? LEARN the Step-by-Step UI BUILDING Guide!</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Fri, 05 Jul 2024 05:14:33 +0000</pubDate>
      <link>https://dev.to/itric/thinking-in-react-a-guide-to-building-user-interfaces-jp3</link>
      <guid>https://dev.to/itric/thinking-in-react-a-guide-to-building-user-interfaces-jp3</guid>
      <description>&lt;p&gt;To utilize and harness the full power of React, it’s essential to adopt a specific mindset known as "Thinking in React". This approach involves breaking down the UI into components, managing state effectively, and ensuring a seamless data flow. In this article, we’ll explore the principles of Thinking in React and provide a step-by-step guide to building efficient and maintainable user interfaces with a practical example. And along the way we will map it to analogy for better understanding.&lt;/p&gt;

&lt;p&gt;(code used in this article :&lt;a href="https://github.com/code-blogger3/Youtube-demos/blob/main/src/App.jsx" rel="noopener noreferrer"&gt;https://github.com/code-blogger3/Youtube-demos/blob/main/src/App.jsx&lt;/a&gt; )&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Core Principles
&lt;/h2&gt;

&lt;p&gt;Before diving into the practical aspects of Thinking in React, it’s important to grasp the core principles that underpin this methodology:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Component-Based Architecture&lt;/strong&gt;: React promotes the idea of building user interfaces using small, reusable components. Each component encapsulates a piece of the UI, making it easier to manage and maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unidirectional Data Flow&lt;/strong&gt;: Data in React applications flows in one direction, from parent components to child components. This predictable data flow simplifies debugging and enhances the application’s stability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State Management&lt;/strong&gt;: React components can maintain their own state, which represents the dynamic parts of the UI. Understanding how to manage state effectively is crucial for building interactive applications.
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/RoRm6QVjkik?start=45"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step-by-Step Guide to Thinking in React
&lt;/h2&gt;

&lt;p&gt;To illustrate the process of Thinking in React, let’s walk through the steps of building a simple searchable product data table.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Break the UI into a Component Hierarchy
&lt;/h3&gt;

&lt;p&gt;The first step is to break down the UI into a component hierarchy. This involves identifying the distinct parts of the interface and mapping them to components. For our example, the product data table can be divided into the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;FilterableProductTable&lt;/strong&gt;: The main container component.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SearchBar&lt;/strong&gt;: A component for the search input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ProductTable&lt;/strong&gt;: A component that displays the product data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ProductCategoryRow&lt;/strong&gt;: A component for displaying product category headers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ProductRow&lt;/strong&gt;: A component for displaying individual products.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s relate this step to an analogy.&lt;/p&gt;

&lt;p&gt;Imagine you're a child again, surrounded by a sea of colorful Lego bricks. You have a vision in your mind—a towering castle with intricate details, bustling marketplaces, and brave knights. But how do you transform this vision into reality? Well, The answer lies in a step-by-step process, much like Thinking in React. Let’s see how building a complex Lego structure mirrors the process of developing a user interface with React.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Lego Metaphor: Building Block by Block
&lt;/h2&gt;

&lt;p&gt;Breaking the UI into a component Hierarchy is similar to breaking Down the blueprint of the Castle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Breaking Down the Castle (Component Hierarchy)
&lt;/h3&gt;

&lt;p&gt;When you start building your Lego castle, you don't just randomly stick pieces together. Instead, you think about the different parts of the castle: the towers, the walls, the gates, and the moat. In React, this is akin to breaking down your user interface into a component hierarchy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Castle&lt;/strong&gt;: This is your entire application, the big picture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Towers and Walls&lt;/strong&gt;: These represent the major sections of your app, like the header, main content, and footer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Gates and Moat&lt;/strong&gt;: These are smaller parts within those sections, like buttons, forms, and navigation links.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By breaking the castle into manageable parts, you can focus on building one section at a time, ensuring each piece fits perfectly with the others.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Build a Static Version in React
&lt;/h3&gt;

&lt;p&gt;Once the component hierarchy is established, the next step is to build a static version of the UI in React. This involves creating the components and rendering them with static data. At this stage, focus on the layout and structure without adding any interactivity or state management.&lt;/p&gt;

&lt;p&gt;This step corresponds to creating static structures of the castle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Static Structures (Static Version in React)
&lt;/h3&gt;

&lt;p&gt;Before adding the finishing touches and intricate details to your Lego castle, you first build a static version. You assemble the towers, walls, gates, and moat without any moving parts or decorations. In React, this is similar to creating a static version of your UI with components that don't yet have interactivity or dynamic behavior.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lego Bricks&lt;/strong&gt;: These are your React components, each serving a specific purpose.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Building the Structure&lt;/strong&gt;: You put together the components to form the basic layout of your app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This step helps you visualize the overall structure and ensures that all parts fit together seamlessly.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Identify the Minimal (But Complete) Representation of UI State
&lt;/h3&gt;

&lt;p&gt;To add interactivity, it’s essential to identify the minimal set of mutable state that the application needs. For our product data table, the state can be categorized into two main types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search Query&lt;/strong&gt;: The text entered in the search bar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stock Only&lt;/strong&gt;: A boolean value indicating whether to show only products in stock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now this step maps to deciding which bricks can move in castle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deciding Which Bricks Can Move (Identifying State)
&lt;/h3&gt;

&lt;p&gt;Now that your static Lego castle is standing tall, you want to add some excitement—maybe a drawbridge that opens, knights that move, and flags that wave in the wind. To do this, you need to identify which parts of the castle should be dynamic. In React, this involves determining the minimal but complete set of state needed for your app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Lego Parts&lt;/strong&gt;: These are the movable pieces, like the drawbridge and knights.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UI State&lt;/strong&gt;: This is the state in React, such as the current page, user input, or whether a modal is open.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By identifying these dynamic parts, you can focus on making only the necessary pieces interactive, keeping your castle (and your code) efficient and manageable.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Identify Where Your State Should Live
&lt;/h3&gt;

&lt;p&gt;Next, determine which components should own the state. According to the React documentation, state should be owned by the component that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Renders something based on that state.&lt;/li&gt;
&lt;li&gt;Needs to pass the state down to its child components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For our example, the &lt;code&gt;FilterableProductTable&lt;/code&gt; component is the best candidate to hold the state, as it renders both the &lt;code&gt;SearchBar&lt;/code&gt; and the &lt;code&gt;ProductTable&lt;/code&gt; and needs to pass the state to these child components.&lt;/p&gt;

&lt;p&gt;This step overlaps to deciding who will control the drawbridge in the castle, where the state lives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deciding Who Controls the Drawbridge (Where State Lives)
&lt;/h3&gt;

&lt;p&gt;In your Lego castle, someone needs to control the drawbridge. Should it be the knight, the king, or perhaps a hidden mechanism? In React, this is like deciding which component should own the state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Drawbridge Mechanism&lt;/strong&gt;: This represents the state in React.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controller&lt;/strong&gt;: This is the component that manages the state, determining how and when it changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By placing the state in the appropriate component, you ensure a smooth and logical flow of information, making it easier to manage and update your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Add Inverse Data Flow
&lt;/h3&gt;

&lt;p&gt;Finally, add inverse data flow to make the components interactive. This involves passing callback functions from the stateful parent component to the stateless child components. The child components call these functions to update the state in the parent component. For instance, the &lt;code&gt;SearchBar&lt;/code&gt; component will receive a callback from &lt;code&gt;FilterableProductTable&lt;/code&gt; to update the search query and stock-only state.&lt;/p&gt;

&lt;p&gt;This step can be looked as allowing communication between knights.&lt;/p&gt;

&lt;h3&gt;
  
  
  Communicating Between Knights (Adding Inverse Data Flow)
&lt;/h3&gt;

&lt;p&gt;To make your Lego castle come alive, the knights need to communicate with each other—when the drawbridge is down, they charge out; when it's up, they retreat. In React, this involves adding inverse data flow, where child components send data back to their parent components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Knight Communication&lt;/strong&gt;: This is like child components sending information back to their parents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parent Commands&lt;/strong&gt;: The parent component uses this information to update the state and trigger actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This two-way communication ensures that all parts of your castle (and your app) work together harmoniously, creating a dynamic and interactive experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Example: Building the Searchable Product Data Table
&lt;/h2&gt;

&lt;p&gt;Let’s put these principles into practice by building the searchable product data table. Below is a simplified implementation in React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Sample product data&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PRODUCTS&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sporting Goods&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$49.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;Football&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sporting Goods&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$9.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;Baseball&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sporting Goods&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$29.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;Basketball&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Electronics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$99.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;iPod Touch&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Electronics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$399.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;iPhone 5&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;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Electronics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$199.99&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;stocked&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="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="s2"&gt;Nexus 7&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="c1"&gt;// FilterableProductTable Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FilterableProductTable&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilterText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInStockOnly&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SearchBar&lt;/span&gt;
        &lt;span class="na"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;inStockOnly&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onFilterTextChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setFilterText&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onInStockChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setInStockOnly&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ProductTable&lt;/span&gt;
        &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;PRODUCTS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;inStockOnly&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;// SearchBar Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;SearchBar&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onFilterTextChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onInStockChange&lt;/span&gt; &lt;span class="p"&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search..."&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onFilterTextChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt;
          &lt;span class="na"&gt;checked&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onInStockChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        Only show products in stock
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="p"&gt;&amp;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;// ProductTable Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductTable&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inStockOnly&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;rows&lt;/span&gt; &lt;span class="o"&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;lastCategory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&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="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filterText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stocked&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;inStockOnly&lt;/span&gt;&lt;span class="p"&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;lastCategory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ProductCategoryRow&lt;/span&gt;
          &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;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;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ProductRow&lt;/span&gt;
        &lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;lastCategory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;product&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;table&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;thead&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Name&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Price&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;thead&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;tbody&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;tbody&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;table&lt;/span&gt;&lt;span class="p"&gt;&amp;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;// ProductCategoryRow Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductCategoryRow&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt; &lt;span class="na"&gt;colSpan&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;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;// ProductRow Component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductRow&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;product&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stocked&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;product&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="p"&gt;&amp;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;// Render the FilterableProductTable component&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Product Table&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FilterableProductTable&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Additional resource: &lt;a href="https://elementarydot.blogspot.com/2024/07/thinking-in-react-relatable-scenarios.html" rel="noopener noreferrer"&gt;https://elementarydot.blogspot.com/2024/07/thinking-in-react-relatable-scenarios.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactjsdevelopment</category>
      <category>react</category>
      <category>learning</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding Closures in JS (with Examples and Analogies)</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Wed, 26 Jun 2024 03:43:00 +0000</pubDate>
      <link>https://dev.to/itric/understanding-closures-in-js-with-examples-and-analogies-1i35</link>
      <guid>https://dev.to/itric/understanding-closures-in-js-with-examples-and-analogies-1i35</guid>
      <description>&lt;p&gt;[Video version of the post] : &lt;a href="https://www.youtube.com/watch?v=UCk7XcAG_Cs" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=UCk7XcAG_Cs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, in this post, I’ll be going talk about closures in js, my archnemesis 🤥&lt;/p&gt;

&lt;p&gt;The things that I will be covering in this post, are listed in content’s overview:&lt;/p&gt;

&lt;p&gt;Content’s Overview:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Starting analogy&lt;/li&gt;
&lt;li&gt;What is a Closure?&lt;/li&gt;
&lt;li&gt;How Closures Work ?&lt;/li&gt;
&lt;li&gt;Examples&lt;/li&gt;
&lt;li&gt;5 analogies to understand&lt;/li&gt;
&lt;li&gt;Why Are Closures Useful?&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start this video with an analogy to understand Closures.&lt;/p&gt;

&lt;p&gt;Closures:&lt;br&gt;
Imagine you have a secret clubhouse that only you and your friends can access. Inside the clubhouse, you have a bunch of toys and games that you all share. Even if you leave the clubhouse, the toys and games are still there for you and your friends to use whenever you come back. That's kind of like a closure in JavaScript - a function that "remembers" the variables it had access to, even after it's done running.&lt;/p&gt;

&lt;p&gt;You might not understand it now, but just keep these lines in mind. You will get clearer picture later.&lt;/p&gt;

&lt;p&gt;Now that ,If you have ever encountered a situation in JavaScript where a function seems to remember variables from its outer scope, even after that scope is gone? That's closures behind it! &lt;/p&gt;
&lt;h3&gt;
  
  
  What is a Closure?
&lt;/h3&gt;

&lt;p&gt;In JavaScript, a  closure is a special concept which can be seen in play when a function is defined inside another function, where an inner function (usually anonymous) is able to access variables from its enclosing function's scope, even after the outer function has finished executing (running) or has returned.&lt;/p&gt;

&lt;p&gt;The "enclosing function's scope" means the area inside the outer function where its variables and functions are defined. When we talk about an inner function accessing the enclosing function's scope, we mean that the inner function can use the variables and functions from the outer function.&lt;/p&gt;

&lt;p&gt;This creates a "closed-over" environment where the inner function remembers the state of the outer function at the time of its creation. In essence, a closure gives you the ability to create functions with "memory" of their originating environment.&lt;/p&gt;

&lt;p&gt;I hope you’re getting a clearer picture. In simple words, when you create function inside a function and outer function has returned inner function. It can still access variables from the outer function that contains it.&lt;/p&gt;

&lt;p&gt;“Closures remember the outer function scope even after creation time.”&lt;/p&gt;
&lt;h2&gt;
  
  
  How Closures Work ?
&lt;/h2&gt;

&lt;p&gt;When a function is defined, it has access to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Its own variables (I mean variables defined inside the function)&lt;/li&gt;
&lt;li&gt;Global variables&lt;/li&gt;
&lt;li&gt;Variables from the outer function's scope (if the function is nested)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To  better understand the third point :  “A function's scope refers to the area inside the function where its variables and parameters are defined and accessible.”&lt;/p&gt;

&lt;p&gt;The inner function takes / access the variables it needs from the outer function's scope. This is what gives closures their power and flexibility.&lt;/p&gt;

&lt;p&gt;Let's break down this concept with some examples that I found.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1: Basic Closure
&lt;/h3&gt;

&lt;p&gt;Consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&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;outerVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am outside!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&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;outerVariable&lt;/span&gt;&lt;span class="p"&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;innerFunction&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;myClosure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;myClosure&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 'I am outside!'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;outerFunction&lt;/code&gt; defines a variable &lt;code&gt;outerVariable&lt;/code&gt; and a function &lt;code&gt;innerFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;innerFunction&lt;/code&gt; can access &lt;code&gt;outerVariable&lt;/code&gt; because it is defined within the same scope (area inside a function).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;outerFunction&lt;/code&gt; returns &lt;code&gt;innerFunction&lt;/code&gt;, creating a closure.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;myClosure&lt;/code&gt; is called, it retains access to &lt;code&gt;outerVariable&lt;/code&gt; even though &lt;code&gt;outerFunction&lt;/code&gt; has completed execution.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&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;x&lt;/span&gt; &lt;span class="o"&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;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Can access x from the outer function&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;innerFunction&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;myInnerFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;myInnerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;innerFunction&lt;/code&gt; is a closure because it can access the &lt;code&gt;x&lt;/code&gt; variable from the &lt;code&gt;outerFunction&lt;/code&gt; scope, even after &lt;code&gt;outerFunction&lt;/code&gt; has finished executing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Closure with Private Variables
&lt;/h3&gt;

&lt;p&gt;Here's a more complex example:&lt;/p&gt;

&lt;p&gt;Closures are often used to create private variables, allowing controlled access through specific functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createCounter&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;count&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;increment&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;decrement&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;getCount&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="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCounter&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 1&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 2&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 1&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;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;   &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;createCounter&lt;/code&gt; initializes a &lt;code&gt;count&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;It returns an object with methods &lt;code&gt;increment&lt;/code&gt;, &lt;code&gt;decrement&lt;/code&gt;, and &lt;code&gt;getCount&lt;/code&gt; that can modify and access &lt;code&gt;count&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; is private and cannot be accessed directly from outside &lt;code&gt;createCounter&lt;/code&gt;, but the returned methods form a closure that allows controlled access to &lt;code&gt;count&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counterFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialValue&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialValue&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="na"&gt;increment&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;count&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;decrement&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;count&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;getCount&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counterFactory&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="nx"&gt;counter1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;counter1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&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;counter1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counterFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;counter2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrement&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;counter2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;counterFactory&lt;/code&gt; function returns an object with three methods: &lt;code&gt;increment&lt;/code&gt;, &lt;code&gt;decrement&lt;/code&gt;, and &lt;code&gt;getCount&lt;/code&gt;. These methods have access to the &lt;code&gt;count&lt;/code&gt; variable from the &lt;code&gt;counterFactory&lt;/code&gt; scope, even after &lt;code&gt;counterFactory&lt;/code&gt; has finished executing.&lt;/p&gt;

&lt;p&gt;Each call to &lt;code&gt;counterFactory&lt;/code&gt; creates a new closure with its own &lt;code&gt;count&lt;/code&gt; variable. This allows us to create multiple counters with different initial values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Closure with Parameters
&lt;/h3&gt;

&lt;p&gt;Closures can also capture parameters passed to the outer function. Here's how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&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;name&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&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="nx"&gt;name&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="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;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;sayHello&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="c1"&gt;// Output: 'Hello, Alice!'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayGoodbye&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;sayGoodbye&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&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: 'Goodbye, Bob!'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;greet&lt;/code&gt; is a function that takes a &lt;code&gt;message&lt;/code&gt; parameter and returns another function that takes a &lt;code&gt;name&lt;/code&gt; parameter.&lt;/li&gt;
&lt;li&gt;The inner function forms a closure that captures the &lt;code&gt;message&lt;/code&gt; parameter from &lt;code&gt;greet&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;sayHello&lt;/code&gt; and &lt;code&gt;sayGoodbye&lt;/code&gt; are called, they remember the &lt;code&gt;message&lt;/code&gt; passed to &lt;code&gt;greet&lt;/code&gt; and use it along with the &lt;code&gt;name&lt;/code&gt; passed to the inner function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To help you better digest the concept of closures, here are 5 analogies :&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;The Backpack Analogy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you are going on a hike and you pack a backpack with some essential items. During your hike, you can take out these items whenever you need them, regardless of where you are on the trail. Similarly, in JavaScript, a function packs its "backpack" with variables from its enclosing scope. Even when the function is executed outside of its original environment, it still has access to those variables.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createHiker&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="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;supplies&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;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;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;map&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="kd"&gt;function&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="s2"&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="s2"&gt; has these supplies: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;supplies&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="s1"&gt;, &lt;/span&gt;&lt;span class="dl"&gt;'&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="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;hiker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createHiker&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="nf"&gt;hiker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 'Alice has these supplies: water, food, map'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this analogy, &lt;code&gt;supplies&lt;/code&gt; are the items in the backpack, and the function &lt;code&gt;hiker&lt;/code&gt; can access these items even after leaving the &lt;code&gt;createHiker&lt;/code&gt; "starting point."&lt;/p&gt;

&lt;p&gt;Alternative version :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Backpack Analogy&lt;/strong&gt;:&lt;br&gt;
Imagine you're packing a backpack for a trip. The backpack represents the outer function, and the items you pack inside it represent the variables and inner functions. When you close the backpack and walk away, the backpack (outer function) has finished its job, but the items inside (the variables and inner functions) are still accessible to you. This is similar to how a closure "closes over" the variables it needs from the outer function, even after the outer function has finished executing.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;The Cookie Jar Analogy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Think of a child reaching into a cookie jar that is placed on a high shelf by a parent. The child remembers where the jar is and can grab cookies whenever they want. In JavaScript, the parent function sets up a "cookie jar" (variables), and the inner function (child) can access the jar even after the parent function is no longer in the picture.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createCookieJar&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;cookies&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;chocolate chip&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;oatmeal raisin&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;peanut butter&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&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;cookies&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;getCookies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCookieJar&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;getCookies&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// Output: ['chocolate chip', 'oatmeal raisin', 'peanut butter']&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, the inner function &lt;code&gt;getCookies&lt;/code&gt; remembers the &lt;code&gt;cookies&lt;/code&gt; variable (the cookie jar) and can access it anytime, even though &lt;code&gt;createCookieJar&lt;/code&gt; has finished executing.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;The Library Card Analogy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you have a library card that gives you access to the library's resources. Even if you leave the library, you can come back and use your card to check out books. Similarly, in JavaScript, an inner function gets a "library card" to access the outer function's variables and can use this access even after the outer function has returned.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;library&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;books&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;Moby Dick&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;Hamlet&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;1984&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="kd"&gt;function&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="s2"&gt;`Available books: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;books&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="s1"&gt;, &lt;/span&gt;&lt;span class="dl"&gt;'&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="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;accessLibrary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;library&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;accessLibrary&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 'Available books: Moby Dick, Hamlet, 1984'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this analogy, the &lt;code&gt;books&lt;/code&gt; are the library's resources, and the inner function &lt;code&gt;accessLibrary&lt;/code&gt; retains the "library card" to access these resources even after the &lt;code&gt;library&lt;/code&gt; function has executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  4 . &lt;strong&gt;The Mailing Package Analogy&lt;/strong&gt;:
&lt;/h3&gt;

&lt;p&gt;Suppose you need to mail a package to a friend. The process of packing the box and sealing it represents the outer function, and the contents of the box represent the variables and inner functions. Once the package is sealed, you can write the address on it and send it off. The address function (inner function) has access to the contents of the package (variables from the outer function), even though the packing process (outer function) is complete.&lt;/p&gt;

&lt;h3&gt;
  
  
  5 . &lt;strong&gt;The Nested Doll Analogy&lt;/strong&gt;:
&lt;/h3&gt;

&lt;p&gt;Imagine a set of Russian nesting dolls, where each doll contains a smaller doll inside it. The outer doll represents the outer function, and the inner dolls represent the variables and inner functions. When you open the outer doll, you can access the inner dolls, just like how a closure allows the inner function to access the variables from the outer function's scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Are Closures Useful?
&lt;/h3&gt;

&lt;p&gt;Closures are incredibly useful in JavaScript for several reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Data privacy and Encapsulation&lt;/strong&gt;: They allow you to create private variables that can't be accessed directly from outside the function, promoting data encapsulation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partial application and currying&lt;/strong&gt;: Closures can be used to create functions that remember some of their arguments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functional Programming&lt;/strong&gt;: Closures are a key feature in functional programming, enabling higher-order functions, currying, and other advanced functional techniques.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memoization&lt;/strong&gt;: Closures can be used to cache the results of expensive function calls and return the cached result when the same inputs occur again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function Factories:&lt;/strong&gt; Closures can be used to generate functions with pre-configured settings, creating a sort of function factory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Handling and callbacks&lt;/strong&gt;: They are often used in event handlers and callback functions, maintaining access to variables even when the context changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Listeners:&lt;/strong&gt; Closures are commonly used in event listeners to capture the state of variables at the time the listener is attached. And to maintain access to variables from the surrounding scope, even after the outer function has finished executing. This is particularly useful for creating more dynamic and flexible event handling.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Closures might seem complex at first, but with practice, they become a valuable tool in your JavaScript development arsenal. By understanding how inner functions access variables from outer scopes, you can create versatile and well-structured code. &lt;/p&gt;

&lt;p&gt;Closures are a powerful feature of JavaScript that enable functions to have private variables and retain access to their originating scope. By understanding and leveraging closures, you can write more modular, encapsulated, and expressive JavaScript code. Whether you're creating private variables, building higher-order functions, or handling events.&lt;/p&gt;

&lt;p&gt;So, the next time you encounter a function with a surprising memory, remember, it might just be a closure in play!&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>A better way to learn programming.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Tue, 25 Jun 2024 04:36:46 +0000</pubDate>
      <link>https://dev.to/itric/a-better-way-to-learn-programming-h6l</link>
      <guid>https://dev.to/itric/a-better-way-to-learn-programming-h6l</guid>
      <description>&lt;p&gt;In this post, I will be talking about two methods to learn how to program.&lt;/p&gt;

&lt;p&gt;The first method (common method) is a step-by-step tutorial approach like following a tutorial. It goes something like this: "Step one, create this component." The tutorial then provides you with detailed instructions on how to create that specific component. "Step two, do this," and you follow the next set of instructions. And when instructions finish you study and repeat the same instruction. This method gives you precise steps to follow. However, the issue here is that the person who created these steps went through a trial-and-error process themselves. They tried something, it didn't work, so they tried something else. They repeated this process until they found a solution that worked. What you're doing by following these steps is simply replicating their final result, which doesn't foster deep learning or problem-solving skills.&lt;/p&gt;

&lt;p&gt;Second method:&lt;/p&gt;

&lt;p&gt;The better way to learn programming involves a more exploratory approach. Instead of providing exact steps, a good teaching method will explain concepts like, "Here’s how you create a component," and, "Here’s how you do this." After explaining these fundamental concepts, the instructor will give you a project, like, "Now, I want you to build X." You attempt to build X on your own, applying the concepts you've learned. At the end, the instructor will show you how they would build X. This method is beneficial because it allows you to struggle and fail, which is where real learning happens. By encountering and overcoming obstacles on your own, you develop a deeper understanding and become a more proficient programmer.&lt;/p&gt;

&lt;p&gt;In my view, this is what learning to program looks like:&lt;/p&gt;

&lt;p&gt;You start by learning a little bit (like a new concept) and trying to apply it. You struggle, you fail, and you try again. You research a lot of things, discovering new concepts and techniques along the way. You then try to arrange all of these new ideas around a specific problem you're trying to solve, making sense of them in your own context. Eventually, you find a way to apply the information you’ve learned, and this is how you grow in understanding and skill.&lt;/p&gt;

&lt;p&gt;An additional note I'd like to share is this: whenever you learn a new piece of information, always ask yourself, "How does this relate to what I already know?" This approach helps you integrate new knowledge with existing understanding, building expertise and deepening your knowledge over time.&lt;/p&gt;

&lt;p&gt;Additionally, I believe the most effective way to learn is not to rely on someone else doing it with you, even though that might seem like a quick way to find the right answer. Instead, it's far more valuable to embrace the process of failing and discovering the context on your own. The essence of learning, especially in programming, lies in grappling with and understanding the complexities of state and state manipulation.&lt;/p&gt;

&lt;p&gt;The real challenge in programming isn't typically about writing functions; those are often straightforward. The difficulty arises in comprehending how the state evolves and flows through the system. This understanding requires extensive debugging and hands-on experience. By immersing yourself in the process and tackling these challenges independently, you build a deeper, more nuanced understanding of how everything fits together. This journey, though time-consuming, ultimately fosters a more robust and enduring grasp of programming concepts.&lt;/p&gt;

&lt;p&gt;Happy coding! with struggle.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>coding</category>
      <category>beginners</category>
    </item>
    <item>
      <title>onClick={someFunction} VS onClick={()=&gt;someFunction}</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Sat, 22 Jun 2024 03:26:45 +0000</pubDate>
      <link>https://dev.to/itric/onclicksomefunction-vs-onclicksomefunction-5d1i</link>
      <guid>https://dev.to/itric/onclicksomefunction-vs-onclicksomefunction-5d1i</guid>
      <description>&lt;p&gt;The difference between &lt;code&gt;onClick={someFunction}&lt;/code&gt; and &lt;code&gt;onClick={() =&amp;gt; someFunction}&lt;/code&gt; in React (or JavaScript in general) lies in how and when they handle the execution of the &lt;code&gt;someFunction&lt;/code&gt; when the click event occurs :&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/rBv6yh-ffes"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;onClick={someFunction}&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Direct Reference&lt;/strong&gt;: This syntax directly references the function &lt;code&gt;someFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When Clicked&lt;/strong&gt;: The function &lt;code&gt;someFunction&lt;/code&gt; will be called when the &lt;code&gt;onClick&lt;/code&gt; event is triggered (e.g., when the element is clicked).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Extra Function Call&lt;/strong&gt;: No additional function is created; React simply uses the function reference you provided.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: If &lt;code&gt;someFunction&lt;/code&gt; is defined as &lt;code&gt;function someFunction() { console.log('clicked'); }&lt;/code&gt;, then &lt;code&gt;onClick={someFunction}&lt;/code&gt; will log 'clicked' when the element is clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;onClick={() =&amp;gt; someFunction()}&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrow Function&lt;/strong&gt;: This syntax uses an arrow function to call &lt;code&gt;someFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creates a New Function&lt;/strong&gt;: Each time the component renders, a new function is created. The arrow function wraps the call to &lt;code&gt;someFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Immediate Invocation&lt;/strong&gt;: Within the arrow function, &lt;code&gt;someFunction&lt;/code&gt; is called immediately when the &lt;code&gt;onClick&lt;/code&gt; event is triggered.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;: Useful when you need to pass arguments to the function or need to do additional work before calling &lt;code&gt;someFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: If you want to pass an argument to &lt;code&gt;someFunction&lt;/code&gt;, you can use &lt;code&gt;onClick={() =&amp;gt; someFunction('argument')}&lt;/code&gt;. This will call &lt;code&gt;someFunction&lt;/code&gt; with 'argument' when the element is clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use Each
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Direct Reference (&lt;code&gt;onClick={someFunction}&lt;/code&gt;)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Use this when you want to avoid creating an extra function on each render, which can be more efficient.&lt;/li&gt;
&lt;li&gt;Preferable for simple event handlers where no additional arguments or operations are needed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Arrow Function (&lt;code&gt;onClick={() =&amp;gt; someFunction()}&lt;/code&gt;)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Use this when you need to pass arguments to the function or perform additional operations before calling the function.&lt;/li&gt;
&lt;li&gt;Useful for inline operations or when dealing with closures.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Practical Examples
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Direct Reference
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;handleClick&lt;/code&gt; will be called directly when the button is clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arrow Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The arrow function calls &lt;code&gt;handleClick&lt;/code&gt; with the argument 'Button clicked' when the button is clicked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these differences helps in optimizing performance and achieving the desired behavior in React components.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>3. Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Wed, 19 Jun 2024 15:35:57 +0000</pubDate>
      <link>https://dev.to/itric/3-building-javascript-array-methods-from-scratch-in-2024-easy-tutorial-for-beginners-4b45</link>
      <guid>https://dev.to/itric/3-building-javascript-array-methods-from-scratch-in-2024-easy-tutorial-for-beginners-4b45</guid>
      <description>&lt;p&gt;Video version's link: &lt;a href="https://www.youtube.com/watch?v=cJpmCjRJB3A" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=cJpmCjRJB3A&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb3mp6pfyaj0do69617f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb3mp6pfyaj0do69617f.png" alt="Image description" width="635" height="357"&gt;&lt;/a&gt;&lt;br&gt;
Continuing our series of building javaScript method from Scratch for beginners from javaScript programming basics. In this post we will be making these 2 method as a function:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array.pop()&lt;/li&gt;
&lt;li&gt;Array.at()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and to make these methods we are allowed to use method that we have already build ,so let’s start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0ewxgwwm2jxb0xw9je7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0ewxgwwm2jxb0xw9je7.png" alt="Image description" width="528" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array.pop():&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First is Array.pop() method. The &lt;strong&gt;pop&lt;/strong&gt; method of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;Array&lt;/a&gt; instances removes the &lt;strong&gt;last&lt;/strong&gt; element from an array and returns that element. This method changes the length of the array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zbl7409bgile6u2bsd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zbl7409bgile6u2bsd2.png" alt="Image description" width="575" height="323"&gt;&lt;/a&gt;&lt;br&gt;
By examining the example input, output, and the method's definition, to build this method as a function we have to somehow extract array’s last element and return it. And removing the last element means after the pop method is executed array will be missing it’s last position element.&lt;/p&gt;

&lt;p&gt;So these are the two things we are focusing on, first extracting last element, extracting element from array requires element’s position index. Now let’s take simplest case possible, array with only one element. We extract last element of that kind of array by taking index as 0 . And for array containing 2 elements, we can extract last element by taking index as 1 and for 3 element containing array the index would be 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8frbqvf6rkunwzj715xk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8frbqvf6rkunwzj715xk.png" alt="Image description" width="633" height="356"&gt;&lt;/a&gt;&lt;br&gt;
Now, Can you see the pattern here? To extract the last element of the array, we need an index which is length of an array minus 1.&lt;/p&gt;

&lt;p&gt;Now the second constraint. For second constraint which is to remove the last element of an array and adjusting the length of an array. Well, listen me out for this one, what if i say adjusting length of an array which is decreasing the length of a JavaScript array by 1 can automatically remove the last element.&lt;/p&gt;

&lt;p&gt;Let me explain: When you decrease the length of a JavaScript array by 1, it effectively removes the last element from the array. This is because the length property of an array in JavaScript is not just a property that you can read, but also a property you can write to. By setting a new length for the array, you can effectively truncate the array, removing elements that are beyond the new length.&lt;/p&gt;

&lt;p&gt;When you change the length of the array, JavaScript automatically removes any elements that are beyond the new length. So, if the original array was [1, 2, 3] and you set arr.length = 2, the array becomes [1, 2]. The element 3 is no longer part of the array.&lt;/p&gt;

&lt;p&gt;So now the all the missing pieces are here, let make a rough draft of our algorithm which is called pseudocode. First, initialize a function, let’s name it customPop. It’s excepts one argument: an array. Second, Extract last element of an array by assigning it to a variable “lastElement”, a reasonable name. Then decrease the length property of that array by 1. And finally, return variable “lastElement”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize a function named customPop, parameter ; arr&lt;/li&gt;
&lt;li&gt;Initialize a variable lastElement&lt;/li&gt;
&lt;li&gt;Assign last element of an array to variable lastElement&lt;/li&gt;
&lt;li&gt;Minus the length property of that array by 1&lt;/li&gt;
&lt;li&gt;return variable lastElement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All done, but one case that we have not take care of is when, given array is empty, meaning array’s length is 0, in that case we have to return undefined.&lt;/p&gt;

&lt;p&gt;modifying the algorithm, after initializing the function, check if array’s length is zero, if yes then return undefined.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize a function named customPop, parameter ; arr&lt;/li&gt;
&lt;li&gt;check if array’s length is zero&lt;/li&gt;
&lt;li&gt;if yes then return undefined&lt;/li&gt;
&lt;li&gt;Initialize a variable lastElement&lt;/li&gt;
&lt;li&gt;Assign last element of an array to variable lastElement&lt;/li&gt;
&lt;li&gt;Minus the length property of that array by 1&lt;/li&gt;
&lt;li&gt;return variable lastElement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here a flowchart representation of our algorithm:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizedgh3tgtu1qpam173u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizedgh3tgtu1qpam173u.png" alt="Image description" width="582" height="327"&gt;&lt;/a&gt;&lt;br&gt;
Now let’s code it up with javaScript:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxd8wgzqffa3yymbmx4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxd8wgzqffa3yymbmx4g.png" alt="Image description" width="629" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8doxjbcv8vlgzvos05xn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8doxjbcv8vlgzvos05xn.png" alt="Image description" width="610" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l4xy7p9rgxqcan40896.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l4xy7p9rgxqcan40896.png" alt="Image description" width="601" height="338"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;customPop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// If the array is empty, return undefined&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;lastElement&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="c1"&gt;// Get the last element&lt;/span&gt;
    &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="nx"&gt;length&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="c1"&gt;// Decrease the length of the array by 1, effectively removing the last element&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lastElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Return the removed element&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;array&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="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;customPop&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="c1"&gt;// Output: 3&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;array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: [1, 2]&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;customPop&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="c1"&gt;// Output: 2&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;array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: [1]&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;customPop&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="c1"&gt;// Output: 1&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;array&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="nf"&gt;customPop&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="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check if the Array is Empty&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the array has no elements (&lt;code&gt;arr.length === 0&lt;/code&gt;), return &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Get the Last Element&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Store the last element of the array in a variable (&lt;code&gt;lastElement&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove the Last Element&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Decrease the length of the array by 1 (&lt;code&gt;arr.length = arr.length - 1&lt;/code&gt;). This effectively removes the last element from the array.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return the Removed Element&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Return the stored last element (&lt;code&gt;lastElement&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This custom &lt;code&gt;pop&lt;/code&gt; function works similarly to the built-in &lt;code&gt;pop&lt;/code&gt; method, adjusting the array's length to remove the last element and returning that element.&lt;br&gt;
&lt;strong&gt;2. Array.at()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next one on the line is &lt;a href="http://array.at/" rel="noopener noreferrer"&gt;array.at&lt;/a&gt; method. The &lt;strong&gt;at&lt;/strong&gt; method of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;Array&lt;/a&gt; instances takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ra15s9q2gg5c4gsws8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ra15s9q2gg5c4gsws8q.png" alt="Image description" width="553" height="311"&gt;&lt;/a&gt;&lt;br&gt;
Well, if given index is an positive integer and is within the bound of an array then its easy, just access a element and return. Problem arises when given index is not an integer, and if it is, it is not positive integer. Remember we can only access element in array with positive integer value. And another constraint is that index have to be within the bound of an array.&lt;/p&gt;

&lt;p&gt;So let’s tackle these constraints one by one, first check if given index is an integer, otherwise return undefined. We can check if given index is an integer by using Number.isInteger method, which is a static method determines whether the passed value is an integer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntpafiu343i6wy2fs3pi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fntpafiu343i6wy2fs3pi.png" alt="Image description" width="548" height="308"&gt;&lt;/a&gt;&lt;br&gt;
Secondly, for negative index passed down to a function, we have to convert it into it’s positive equivalent index. So how would we do it? again, let’s start by taking simplest case possible, array with only one element. In this array, we can only access one element, the 0 index element which corresponding negative index is -1. And how we convert -1 to 0? simple by adding 1. And in array with 2 elements, 0 index corresponding negative index is -2.&lt;/p&gt;

&lt;p&gt;Can you see the pattern here? As length of an array increase, 0 index corresponding negative index decreases. So, To get positive index of an element from negative index, just add it with length of an array.&lt;/p&gt;

&lt;p&gt;And importantly, we have to convert negative index into positive before this step, as it is easier check this condition with positive index. And if index is not within the bound we will return undefined.&lt;/p&gt;

&lt;p&gt;Now let’s write our algorithm of custom at function in simple English sentences: First we initialize a function with two parameters; array and index. Let’s name it customAt. And this time let’s start with checking if given array is of type array, as i have shown you how to do it, quite a few times in this series. And if given array is not of type array then throw an appropriate error.&lt;/p&gt;

&lt;p&gt;Secondly, check if given index is an integer, if not then return undefined. Next, check if given index is negative, if yes then add array’s length to it. Next, check if index is within the bound of array, if not then return undefined. And finally, return required element corresponding array’s index.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize function named customAt, parameters ; array and index&lt;/li&gt;
&lt;li&gt;check if array is of type array&lt;/li&gt;
&lt;li&gt;If not, then throw an appropriate error&lt;/li&gt;
&lt;li&gt;check if given index is an integer&lt;/li&gt;
&lt;li&gt;if not then return undefined&lt;/li&gt;
&lt;li&gt;check if given index is negative&lt;/li&gt;
&lt;li&gt;if yes then add array’s length to it&lt;/li&gt;
&lt;li&gt;check if index is within the bound of array&lt;/li&gt;
&lt;li&gt;if not then return undefined&lt;/li&gt;
&lt;li&gt;return required element corresponding array’s index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here a flowchart representation of our algorithm:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdrh40gsgzmms0i5vus5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdrh40gsgzmms0i5vus5.png" alt="Image description" width="601" height="338"&gt;&lt;/a&gt;&lt;br&gt;
Now let’s code it up with javaScript:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40f2p8fz80q1zmfcekok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40f2p8fz80q1zmfcekok.png" alt="Image description" width="580" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvst0i5og8itzb87i79on.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvst0i5og8itzb87i79on.png" alt="Image description" width="610" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7mbh6new1hi4f7uyt271.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7mbh6new1hi4f7uyt271.png" alt="Image description" width="686" height="386"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;customAt&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="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Check if the input is an array&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&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="nf"&gt;isArray&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="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The first argument must be an array&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="c1"&gt;// Check if the index is an integer&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The index must be an integer&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="c1"&gt;// Handle negative indices&lt;/span&gt;
  &lt;span class="k"&gt;if &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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&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="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Check if the index is within bounds&lt;/span&gt;
  &lt;span class="k"&gt;if &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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&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;array&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&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="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="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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="c1"&gt;// Output: 3&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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&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="c1"&gt;// Output: 5&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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="c1"&gt;// Output: 3&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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="c1"&gt;// Output: undefined&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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&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;customAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: Error: The index must be an integer&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Input Validation&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The function first checks if the input is an array using &lt;code&gt;Array.isArray()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the input is not an array, a &lt;code&gt;TypeError&lt;/code&gt; is thrown.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integer Check&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The function checks if the index is an integer using &lt;code&gt;Number.isInteger()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the index is not an integer, a &lt;code&gt;TypeError&lt;/code&gt; is thrown.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Negative Index Handling&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the index is negative, it is adjusted by adding the array’s length to it (&lt;code&gt;index += array.length&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bounds Checking&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The function checks if the adjusted index is within the bounds of the array.&lt;/li&gt;
&lt;li&gt;If the index is out of bounds (either less than 0 or greater than or equal to the array’s length), the function returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Element Retrieval&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If the index is within bounds, the function returns the element at the specified index.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This implementation ensures that the index must be an integer and provides the same functionality as the &lt;code&gt;Array.prototype.at()&lt;/code&gt; method, allowing for both positive and negative indexing and robust error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Thank you for reading, that's all for today.&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>2. Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners.</title>
      <dc:creator>itric</dc:creator>
      <pubDate>Wed, 19 Jun 2024 08:05:48 +0000</pubDate>
      <link>https://dev.to/itric/2-building-javascript-array-methods-from-scratch-in-2024-easy-tutorial-for-beginners-4apf</link>
      <guid>https://dev.to/itric/2-building-javascript-array-methods-from-scratch-in-2024-easy-tutorial-for-beginners-4apf</guid>
      <description>&lt;p&gt;Video version link: &lt;a href="https://www.youtube.com/watch?v=3DE4-o23tPc" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=3DE4-o23tPc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ffn7974atr8i9m06qr2r5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ffn7974atr8i9m06qr2r5.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Continuing our series of building javaScript method from Scratch for beginners, in this blog we will be making these 2 methods : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Array.push()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Array.concat()&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and to make these methods we are allowed to use method that we have already build ,so let’s start.&lt;br&gt;
Same as previous post,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F66n35bb6eftgsw33vy3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F66n35bb6eftgsw33vy3s.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
First i will be introducing the array method with their example input and output, then we will examine those methods, identifying which programming tools and tricks to used and in which order to arrange them. Along that process we will build pseudocode algorithm for required function and later on, i will show you flowchart version of that algorithm, the important tip that i can give to you all, to get most out of this blog, is to first try to build it by yourself then come to this blog.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array.push():&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First is Array.push() method, The push method of array instances adds the specified elements to the end of an array and returns the new length of the array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fw5nmxbvya3wxgbkygbnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fw5nmxbvya3wxgbkygbnf.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
So, from looking at example input and output and definition of the method , we have to somehow calculate length of array and then return it. Well, remember we could use methods that we have already build, so we could use length method, actually property to find length of array.&lt;/p&gt;

&lt;p&gt;Next, we have to add given element to the end of an array, but we can’t use push method so, how? Well, let’s go to basics, remember:&lt;/p&gt;

&lt;p&gt;" JavaScript programming language supports dynamic arrays, which means that the array data structure can automatically adjust its size to accommodate new elements as they are added. This capability allows for flexible and efficient management of collections of data without needing to manually allocate and manage memory or resize the array."&lt;/p&gt;

&lt;p&gt;Supporting dynamic arrays means that the array data structure can automatically adjust its size to accommodate new elements as they are added. This capability allows for flexible and efficient management of collections of data without needing to manually allocate and manage memory or resize the array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of Dynamic Arrays:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Automatic Resizing&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Dynamic arrays grow or shrink automatically as elements are added or removed. This is in contrast to static arrays, which have a fixed size that cannot be changed after they are created.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Memory Management&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Dynamic arrays manage memory allocation and reallocation internally. When the array reaches its capacity, it typically allocates a larger block of memory, copies the existing elements to the new block, and then continues adding new elements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Use&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Users can add, remove, or access elements without worrying about the underlying memory management. This makes dynamic arrays much easier to use compared to static arrays, especially when the number of elements can vary.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Examples in Various Languages:
&lt;/h3&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;JavaScript arrays are inherently dynamic:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
arr.push(4);  // Adds an element to the end of the array
console.log(arr);  // Output: [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Python&lt;br&gt;
Python lists are dynamic:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3]
arr.append(4)  # Adds an element to the end of the list
print(arr)  # Output: [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Java&lt;br&gt;
Java's ArrayList is a dynamic array:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList&amp;lt;Integer&amp;gt; arr = new ArrayList&amp;lt;&amp;gt;();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);  // Adds an element to the end of the ArrayList
        System.out.println(arr);  // Output: [1, 2, 3, 4]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;C++&lt;br&gt;
C++'s std::vector is a dynamic array:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;

int main() {
    std::vector&amp;lt;int&amp;gt; arr = {1, 2, 3};
    arr.push_back(4);  // Adds an element to the end of the vector
    for (int i : arr) {
        std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; " ";
    }
    // Output: 1 2 3 4
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Another thing to note is,&lt;br&gt;
arrays are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on. The length property (or equivalent) of an array provides the total number of elements in the array.&lt;/p&gt;

&lt;p&gt;When you add an element to the end of an array, you place it at the index equal to the current length of the array because:&lt;/p&gt;

&lt;p&gt;1.The indices of the existing elements range from 0 to length - 1.&lt;/p&gt;

&lt;p&gt;2.The next available position is at the index length.&lt;/p&gt;

&lt;p&gt;so, we just have to directly add or assign the element at the length index slash next available index.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fdwbfqg8dgcdl88mcfgmx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fdwbfqg8dgcdl88mcfgmx.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now, its time to make rough draft of our algorithm, first initialize function cause we are making a function, let’s name it push taking two parameters array and element to add. Then assign element to array at next available index that would be length of an array. Finally, return array’s length.&lt;/p&gt;

&lt;p&gt;Pseudocode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize function named push&lt;/li&gt;
&lt;li&gt;assign element to array at next available index (length)&lt;/li&gt;
&lt;li&gt;return array’s length&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fva3d092p94p6zcl45s5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fva3d092p94p6zcl45s5d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here a flowchart representation of our algorithm: nothing complex&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fjbl6v9sk16yz1g7wrcoe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fjbl6v9sk16yz1g7wrcoe.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now let’s code it up with javaScript:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqyvp897rhqt8mctpd7xx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqyvp897rhqt8mctpd7xx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqytqky0ao3rmf5rcp4bm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqytqky0ao3rmf5rcp4bm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fq7td5t0m3yjncur7hefn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fq7td5t0m3yjncur7hefn.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
In this implementation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The new element &lt;code&gt;ele&lt;/code&gt; is added at the index &lt;code&gt;arr.length&lt;/code&gt;, which is the next available position in the array.&lt;/li&gt;
&lt;li&gt;The length of the array is incremented automatically.&lt;/li&gt;
&lt;li&gt;The function returns the new length of the array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This custom &lt;code&gt;push&lt;/code&gt; function works similarly to the native &lt;code&gt;Array.prototype.push&lt;/code&gt; method. It modifies the original array and returns the new length of the array.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Array.concat():&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next is array’s concat method. The &lt;strong&gt;concat&lt;/strong&gt; method of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;Array&lt;/a&gt; instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fpgajtbs6k4t6i2il7lgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fpgajtbs6k4t6i2il7lgh.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
So, how can we merge two arrays? If you think about it , merging an arrays is same as, taking individual elements and placing it in correct order into bigger array.&lt;/p&gt;

&lt;p&gt;So, next logical step would be, if somehow expands the arrays into individual elements. And place them into larger array, then return that. that could work. &lt;/p&gt;

&lt;p&gt;Now, the question is how to expands the arrays into individual elements? Well, let’s go home again, i mean the basics. In array, we have study about spread operator, which does exactly what we need, which is to expands the array into individual elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fixg29xjeptqpufg4crc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fixg29xjeptqpufg4crc1.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
and to concatenate, place the spread arrays inside square brackets ,we create a new array that includes all elements from both arrays.&lt;/p&gt;

&lt;p&gt;Now, time for annoying part or for some or most, the fun part, edge case. What if passed down arguments aren’t of type array. For that, just throw an error.&lt;/p&gt;

&lt;p&gt;so, updating our algorithm, check if both arrays are of type array. If not, then throw an appropriate error like “Both arguments should be arrays”.&lt;/p&gt;

&lt;p&gt;Pseudocode :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;initialize function named concat.&lt;/li&gt;
&lt;li&gt;check if both arrays are of type array&lt;/li&gt;
&lt;li&gt;If not, then throw an appropriate error&lt;/li&gt;
&lt;li&gt;return a array inside which, two spread array in placed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fr1t05io7od1t1wsli0kn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fr1t05io7od1t1wsli0kn.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here a flowchart representation of our algorithm: nothing complex&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fv014cmr3k93d3gjzwqbq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fv014cmr3k93d3gjzwqbq.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now let’s code it up with javaScript:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fhjpik9sv32u4tfo4y5q0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhjpik9sv32u4tfo4y5q0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F86zd91kwq5nwnwbtvkef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F86zd91kwq5nwnwbtvkef.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F5l7pfpcn1e2e70uy7rrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F5l7pfpcn1e2e70uy7rrn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The spread operator (...) in JavaScript allows an iterable such as an array to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. This makes it a powerful tool for array manipulation, including concatenation.&lt;/p&gt;

&lt;p&gt;How the Spread Operator Works&lt;br&gt;
When you use the spread operator with arrays, it effectively breaks down the array into individual elements. This can then be used to create a new array containing elements from multiple arrays.&lt;/p&gt;

&lt;p&gt;Example of Spread Operator for Concatenation&lt;br&gt;
Here's an example to illustrate how the spread operator works for array concatenation:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3];
const arr3 = [4, 5, 6];

// Using the spread operator to concatenate arrays
const concatenatedArray = [...arr, ...arr3];

console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step-by-Step Explanation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Spread Syntax&lt;/strong&gt;: The spread operator &lt;code&gt;...arr&lt;/code&gt; expands the array &lt;code&gt;arr&lt;/code&gt; into individual elements. Similarly, &lt;code&gt;...arr3&lt;/code&gt; expands &lt;code&gt;arr3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concatenation&lt;/strong&gt;: By placing the spread arrays inside square brackets &lt;code&gt;[ ...arr, ...arr3 ]&lt;/code&gt;, we create a new array that includes all elements from both &lt;code&gt;arr&lt;/code&gt; and &lt;code&gt;arr3&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Detailed Breakdown
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initial Arrays&lt;/strong&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const arr = [1, 2, 3];&lt;br&gt;
const arr3 = [4, 5, 6];&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Using Spread Operator&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;concatenatedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is equivalent to:&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;concatenatedArray&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;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class="highlight jsx"&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;concatenatedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5, 6]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Implementation in a Function
&lt;/h3&gt;

&lt;p&gt;Here's the complete code including a function to concatenate arrays with input validation:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Check if both inputs are arrays&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&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="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&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="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondArray&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Both arguments should be arrays&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="c1"&gt;// Concatenate arrays using the spread operator&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;secondArray&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="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;arr3&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="mi"&gt;6&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;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5, 6]&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;By leveraging the spread operator, array concatenation becomes more straightforward and intuitive.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Thank&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;reading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;s all for today.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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