<?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: Darenporretto</title>
    <description>The latest articles on DEV Community by Darenporretto (@darenporretto).</description>
    <link>https://dev.to/darenporretto</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%2F2396312%2F5e884f32-4c15-4707-9fc5-e8a945a8fba9.jpg</url>
      <title>DEV Community: Darenporretto</title>
      <link>https://dev.to/darenporretto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darenporretto"/>
    <language>en</language>
    <item>
      <title>Recursion in JavaScript</title>
      <dc:creator>Darenporretto</dc:creator>
      <pubDate>Fri, 04 Apr 2025 13:58:38 +0000</pubDate>
      <link>https://dev.to/darenporretto/recursion-in-javascript-di0</link>
      <guid>https://dev.to/darenporretto/recursion-in-javascript-di0</guid>
      <description>&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Recursion" rel="noopener noreferrer"&gt;Recursion&lt;/a&gt; is a powerful programming concept and when we fully understand, it can greatly enhance our problem solving skills. Though it can seem intimidating at first, especially for beginners, recursion is a concept that is way simpler than it looks. In this post, let's explore what recursion is, why it's useful, and how we can start implementing it in JavaScript.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is Recursion?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursion is a technique where a function calls itself to solve a problem. A method of breaking down a larger task into smaller sub tasks. When a function calls itself, it’s usually solving a smaller instance of the same problem until it reaches a base case, which is a condition where the recursion stops.&lt;/p&gt;

&lt;p&gt;For example, think of it like a set of &lt;a href="https://medium.com/analytics-vidhya/recursion-the-nesting-doll-of-programming-404ae61708a0" rel="noopener noreferrer"&gt;Russian nesting dolls&lt;/a&gt;: you open one to find another smaller doll inside, and you repeat this process until you reach the smallest doll.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Basic Structure of Recursion&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A recursive function generally has two parts:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;~&lt;a href="https://stackoverflow.com/questions/32029430/javascript-recursion-base-case" rel="noopener noreferrer"&gt;Base case&lt;/a&gt;&lt;/strong&gt;: This is the condition that ends the recursion. Without a base case, a recursive function will call itself indefinitely and cause a stack overflow which is a program crash.&lt;br&gt;
&lt;strong&gt;~&lt;a href="https://medium.com/@roshan.waa/understanding-recursion-in-javascript-a-step-by-step-guide-with-factorial-example-and-how-it-works-aaa4ebbb4d0d#:~:text=In%20JavaScript%2C%20recursion%20is%20implemented,a%20case%20that%20calls%20itself." rel="noopener noreferrer"&gt;Recursive case&lt;/a&gt;&lt;/strong&gt;: This is the part where the function calls itself with adjusted arguments, that gradually move towards the base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function factorial(n) {
  // Base case
  if (n === 0) {
    return 1;
  }
  // Recursive case
  return n * factorial(n - 1);
}

console.log(factorial(5)); // Output: 120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;*The base case is &lt;code&gt;n === 0&lt;/code&gt; where the factorial of 0 is defined as 1.&lt;/p&gt;

&lt;p&gt;*The recursive case is the function calling itself with &lt;code&gt;n - 1&lt;/code&gt;, reducing the problem until it hits the base case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why is Recursion Useful?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursion is a refined way of solving problems that have a repetitive structure. For example:&lt;/p&gt;

&lt;p&gt;Recursion is great for problems like calculating &lt;a href="https://www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/" rel="noopener noreferrer"&gt;factorials&lt;/a&gt;, &lt;a href="https://codedamn.com/news/javascript/fibonacci-series-in-javascript" rel="noopener noreferrer"&gt;Fibonacci sequences&lt;/a&gt; , as well as traversing mathematical functions that will repeat themselves in smaller units.&lt;/p&gt;

&lt;p&gt;Let’s take a look at a classic example: calculating the Fibonacci sequence. The Fibonacci sequence is defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*In JavaScript we can execute this as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fibonacci(n) {
  // Base cases
  if (n === 0) return 0;
  if (n === 1) return 1;

  // Recursive case
  return fibonacci(n - 1) + fibonacci(n - 2);
}

console.log(fibonacci(6)); // Output: 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Here every call to fibonacci calculates smaller &amp;amp; smaller versions of the problem until it reaches the base cases &lt;br&gt;
&lt;code&gt;(n === 0 or n === 1)&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Imagine you are standing in front of two mirrors facing each other. You see an endless series of reflections within reflections. Essentially this is what recursion does: each function call reflects itself into a smaller problem until a base case is reached, where the process stops. This analogy helps explain the sometimes &lt;em&gt;“infinite”&lt;/em&gt; nature of recursion, as long as there is a condition, the base case, to fully stop the process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;~What Can Go Wrong?~&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even though recursion is a powerful tool, there are potential danger issues:&lt;/p&gt;

&lt;p&gt;~Stack Overflow: If the base case is not correctly defined, or the function does not reduce the problem size sufficiently in every recursive call, the recursion could go on indefinitely and cause a stack overflow. Which is a situation where the call stack exceeds its limit and crashes the program.&lt;/p&gt;

&lt;p&gt;~Efficiency: Recursive solutions are often less efficient than iterative solutions because every recursive call adds a layer to the stack. With problems like the Fibonacci sequence, a poorly structured recursive solution can repeat calculations many times, leading to unnecessary overhead. Here's an example of this inefficiency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fibonacci(n) {
  if (n === 0) return 0;
  if (n === 1) return 1;
  return fibonacci(n - 1) + fibonacci(n - 2); // Inefficient for large n
}

console.log(fibonacci(50)); // This takes a long time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*In cases like this, using memoization approach drastically improves performance. Memoization stores the results of expensive function calls and will return the cached result when the same inputs occur again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;~Improving Efficiency with Memoization~&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/memoization-in-javascript-and-react/" rel="noopener noreferrer"&gt;Memoization&lt;/a&gt; involves storing the result of a function call so that if the function is called again with the same arguments, the cached result is returned, instead of recalculating it. Here’s how we can apply memoization to the Fibonacci function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fibonacci(n, memo = {}) {
  if (n in memo) return memo[n]; // Return cached result
  if (n === 0) return 0;
  if (n === 1) return 1;

  // Store the result in the memo object
  memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
  return memo[n];
}

console.log(fibonacci(50)); // Now it's much faster

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

&lt;/div&gt;



&lt;p&gt;*By using memoization, we store the Fibonacci numbers as they are calculated, and the next time we need them, we can simply look them up in the &lt;code&gt;memo&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;~Embrace the Power of Recursion~&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're just starting with recursion, don’t worry if it feels strange at first. With practice, it will start to make more sense. The key to mastering recursion is to start with simple examples and understand the flow of function calls.  Then gradually increase the complexity of the problems you solve.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/rf60MejMz3E"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In conclusion, recursion is not just a tool, it’s a way of thinking about problems in a recursive manner. Next time you encounter a problem, ask yourself: "Can I break this down into smaller instances of the same problem?" And if the answer is yes, then recursion might be your perfect solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;SOURCES&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a&gt;MDN Web Docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/" rel="noopener noreferrer"&gt;Stack Overflow&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/" rel="noopener noreferrer"&gt;freeCodeCamp&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codedamn.com/" rel="noopener noreferrer"&gt;codedamn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Higher-Order Functions</title>
      <dc:creator>Darenporretto</dc:creator>
      <pubDate>Fri, 15 Nov 2024 18:31:41 +0000</pubDate>
      <link>https://dev.to/darenporretto/higher-order-functions-18bf</link>
      <guid>https://dev.to/darenporretto/higher-order-functions-18bf</guid>
      <description>&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/higher-order-functions-in-javascript-explained/" rel="noopener noreferrer"&gt;Higher-order functions&lt;/a&gt; are a powerful feature of JavaScript, enabling more concise, reusable, and expressive code. If you're looking to improve your JavaScript skills, understanding higher-order functions is essential. Let's explore what they are, how they work, and how you can use them effectively.&lt;/p&gt;

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

&lt;p&gt;~What Are Higher-Order Functions?~&lt;br&gt;
A higher-order function is a function that either:&lt;/p&gt;

&lt;p&gt;1.Takes one or more functions as arguments, or&lt;br&gt;
2Returns a function as its result.&lt;/p&gt;

&lt;p&gt;In other words, higher-order functions can operate on other functions, either by taking them as input or producing them as output.&lt;/p&gt;

&lt;p&gt;~Why Are They Important?~&lt;br&gt;
Higher-order functions allow you to:&lt;/p&gt;

&lt;p&gt;*Write cleaner, modular code.&lt;br&gt;
*Abstract common behavior, reducing repetition.&lt;br&gt;
*Improve code readability and maintainability.&lt;/p&gt;

&lt;p&gt;By mastering them, you can write more efficient and elegant JavaScript code.&lt;/p&gt;

&lt;p&gt;~Common Higher-Order Functions in JavaScript~&lt;br&gt;
JavaScript has several built-in higher-order functions like map(), filter(), and reduce(). These functions are frequently used to transform and manipulate arrays.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;map()&lt;/a&gt;: Transforming Arrays&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;map() applies a function to each element in an array and returns a new array with the results.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;filter()&lt;/a&gt;: Filtering Arrays&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;filter() creates a new array with only the elements that pass a test defined by the provided function.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;reduce()&lt;/a&gt;: Reducing an Array to a Single Value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;reduce() aggregates all elements in an array into a single value, such as a sum or product.&lt;/p&gt;

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

&lt;p&gt;~Conclusion~&lt;br&gt;
Higher-order functions are a key feature of JavaScript that help you write more modular, reusable, and elegant code. Functions like map(), filter(), and reduce() are just a few examples of how higher-order functions can simplify array manipulation.&lt;/p&gt;

&lt;p&gt;Start using higher-order functions in your code to enhance readability and maintainability. With practice, you'll see how they can make your JavaScript code cleaner and more efficient.&lt;/p&gt;

&lt;p&gt;But higher-order functions don’t just streamline array manipulation. They also enable you to create more flexible and reusable code. By writing functions that accept other functions as arguments, you can design code that can be customized and extended without having to modify the underlying logic. For instance, you could write a custom higher-order function to log the results of any array manipulation or to create a new function that checks if all elements in an array meet a certain condition.&lt;/p&gt;

&lt;p&gt;In addition to arrays, higher-order functions are widely used in other areas of JavaScript, such as event handling and asynchronous programming. In functional programming, you'll find that they play a central role in reducing side effects and improving the predictability of your code.&lt;/p&gt;

&lt;p&gt;It's also worth noting that while higher-order functions can be incredibly powerful, they are not a magic solution for all programming problems. It's important to know when and where to use them. In some cases, traditional loops or imperative code might be more efficient or easier to understand, especially in performance critical applications. However, with practice, you'll become better at recognizing opportunities where higher-order functions shine, helping you write cleaner, more maintainable code.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Sources&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/higher-order-functions-in-javascript-explained/" rel="noopener noreferrer"&gt;Free Code Camp&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/javascript-higher-order-functions/" rel="noopener noreferrer"&gt;GeeksforGeeks&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
