This article actually comes courtesy of a LinkedIn connection who decided to try to write a function to find the “nth” integer in the Fibonacci Sequence. I remember in my own boot-camp when we were learning about For Loops and Recursion that this exercise was one of my favorites, but I also remember some classmates trying to use a recursive function for the Fibonacci Sequence while others attempted an iterative method. Now, this begs the question, what is the difference between Iterative and Recursive functions?
An iterative function is one which would iterate over a sequence of numbers or data. It analyzes the data and then moves on to the next piece of data in the sequence until it hits a break statement, or a stack overflow occurs. An easily recognizable example would be a loop, such as a For Loop or a While Loop. Let’s take a look at a For Loop:

This is a For Loop that will count up to the “nth” term in the Fibonacci Sequence, where in the function argument, “n” is the term we are looking for. In the example, we passed 6 as being the 6th term we want, which gives us the following message in the console:

So what exactly happened in this function? The letter “i” is our counter. In the loop, it starts with a value of 0. As long as “i” is less than the number we are looking for, it will increase in value by 1. Above the loop, we’ve already told the function what the first two numbers of the Fibonacci Sequence are, 0 and 1 as “n1” and “n2”. Within the function, we tell it that to proceed to the next term in the loop, it must add “n1” and “n2”, and when it does this, the values of n1 and n2 are adjusted so that it will keep adding the two values that come before the next value. We also have a console.log(n1) command so that we can track the loop’s progress and see the sequence. When the loop reaches the “n” number it has been counting up to, the function ends.
A recursive function is one that will continue to call itself until it doesn’t have to, such as when a condition is met that introduces a break statement. A real life example would be like a roller coaster that goes into an inverted loop. The coaster reaches a point where it actually must go back on itself, and cover the same later distance again before it can continue down the rest of the track. In code, this might look like a countdown timer:

When we first call the function on line 51, we call 7 as an argument. Within the function, we provide an If Statement, that says if the number passed as an argument strictly equals the value of 0, return or break the function. Otherwise, we then console.log() the number so we can track the function, then callback the function countDown() within itself, with the value of the argument minus 1, effectively telling it to repeat but with a smaller number. Once the argument or “num” equals 0, the function stops. Within the console we see the following:

With both iterative and recursive functions, some care must be taken not to create a stack overflow. This occurs when the function is processing so much data that it exceeds the call stack and memory parameters of a browser or computer. As far as which is better, it depends on how one defines, “better.” Various loops are faster than recursive functions and usually easier to read and understand, but they can make code clunky. While recursive functions have to keep calling themselves over and over again within themselves, this can slow down a process, but there is a certain elegance and neatness that you might not find within a For Loop. However, the bigger the number you count down from in our countDown() function, the greater the time complexity will be, because that’s how many more times the function has to run, rather than our For Loop, which will just run once.
As a Junior Developer, I can’t accurately tell you when would need to use one of these methods over the other, or which would be the best choice for a given task. But I sincerely hope this explanation can help out the other Junior Devs out there who might be stumped by these.