I work primarily on application-level programs, so I tend to not use recursion very often. However, every now and then I need a function that is best solved recursively. It is important to be able to , and to when the time comes. recognize when a problem is best solved recursively be able to write it What is Recursion? Recursion is a process in which a . function calls itself For example: { (i === arr.length){ ; } .log(arr[i]) recursive(arr, i+ ) } ( ) function printArrayRecursive arr, i // base case, stop recurring if return console // call ourself with the next index 1 In the code above, prints one element from the list, then calls itself again with the next index. Each successive call to itself prints the next element, and so on. The recursion continues until the is reached. In our example, the base case is when the index is equal to the array's length. printArrayRecursive base case The same function looks quite a bit different in the world, which you are probably more familiar with: iterative { ( i = ; i < arr.length; i++){ .log(arr[i]) } } ( ) function printArrayIterative arr for let 0 console In the case of simply printing the items of a list, the iterative approach is better for a number of reasons: Easier to read and understand Less memory utilization - Recursive functions keep all calls on the stack until the base case is reached Faster compute time - Recursive functions come with the overhead of an entire function call for each step If there is a bug in the recursion, the program is likely to enter an infinite loop So Why Use Recursion? All iterative programs can be written using recursion, and all recursive programs can be written using iteration. This is because both systems are, unless limited by the implementation, . turing complete mechanical turing machine The primary reason to choose recursion over iteration is . simplicity Many years ago many compilers and interpreters didn't support the syntax for iteration. . This is because it is much simpler to write an interpreter that can handle recursion, than it is to write one that supports loops. For-loops simply didn't exist Likewise, even if a compiler does support loops, some problems are simpler to solve with a recursive function. A good example is tree-traversal. I often find myself writing recursive functions to find every property of an arbitrary JSON object, or looking through every file in a folder that can have an infinite number of nested subfolders. Examples Recursively print all properties of a JSON object: { ( k obj) { ( obj[k] === ) { printAllVals(obj[k]) } { .log(obj[k]); } } } } ( ) function printAllVals obj for let in if typeof "object" else // base case, stop recurring console Recursively print all the filenames of a folder, and its subfolders, and their subfolders, ad infinitum. { files = fs.readdirSync(dir); files.forEach( { absName = (fs.statSync(absName).isDirectory()) { printSubFiles(absName) } { .log(file) } }); }; ( ) function printSubFiles dir ( ) function file ` / ` ${dir} ${file} if else // base case, stop recurring console When trying to figure out how to write a function recursively, think, or in other words, "What is my base case?" "What should stop the recursion from continuing?" Once that is hammered out, the rest of the function just needs to answer the questions, "What do I want to do with my current value?" and "How do I call myself to get to the next value?" Recursion is an important principle to understand for any programmer, and I hope this helps you be just a little better! Thanks for reading. Previously published at https://qvault.io/2019/09/22/thinking-about-recursion-how-to-recursively-traverse-json-objects-and-the-filesystem/ Download Qvault: https://qvault.io Star our Github: https://github.com/q-vault/qvault