Recursion is the method by which a problem gets solved through iteration. In other words, a is a function that (or until something stops it). recursive function repetitively invokes itself infinitely This article will use an example to illustrate how a recursive function works. Note: A recursive function is different from an (IIFE). An IIFE automatically invokes itself once. However, a recursive function automatically invokes itself repeatedly for an unlimited amount of time or until something stops its reinvocation. Immediately Invoking function Expression The code written to discontinue the reinvocation of a recursive function is called a . base case It is always important to define a base case when creating a recursive function — so that the recursion function will not run endlessly, thereby crashing the browser. Example of a recursive function Below is a code that outputs a of all the values returned through the function’s recursive invocation. JavaScript concatenation countDown() { (num < ) { ; } num + + countDown(num - ); }; countDown( ); // Create a recursive function: ( ) function countDown num // Define the base case of this recursive function: if 0 return "Recursion Stopped!" // Define the recursive case (recursion statement): return ", " 1 // Invoke the countDown() recursive function: 2 // The invocation above will return: "2, 1, 0, Recursion Stopped!" In the recursive algorithm above, the code makes the whole function a recursion because it is the code that makes recall itself repeatedly. Note: countDown(num - 1) countDown() A look at the events behind the scenes When we invoked the function and passed in the value (that is, ), the algorithm started running as follows: countDown 2 countDown(2) Step 1: Check if 2 is less than 0 The computer checked if the value — that we passed to the parameter of the function — is less than . 2 num countDown 0 Since is not less than , the computer did not execute the statement's code. Instead, it skipped to the next code after the statement — which is the . 2 0 if if recursion code Step 2: Execute the return statement After skipping the statement, the computer executed the code — but substituted the parameter with the parameter’s value (that is, ) like so: if return num + " " + countDown(num - 1) num 2 num + + countDown(num - ); + + countDown( - ); + + countDown( ); return ", " 1 return 2 ", " 2 1 return 2 ", " 1 Step 3: Execute only the recursion code In step 2’s code above, notice that the command cannot return any value because the statement includes a recursion code ( ) recalling the function. return return countDown(1) countDown Therefore, while retaining the other parts of the statement (that is, ), the computer will execute the recursion code ( ). return 2 + ", " + only countDown(1) In other words, the code will automatically invoke the function while passing-in the value . Then, the algorithm will start running again by checking if is less than . countDown(1) countDown 1 1 0 Since is not less than , the computer skipped to the recursion code like so: 1 0 + + num + + countDown(num - ); + + + + countDown( - ); + + + + countDown( ); return 2 ", " ", " 1 return 2 ", " 1 ", " 1 1 return 2 ", " 1 ", " 0 Step 4: Invoke only the recursion code Again, notice that the command (in step 3) cannot return any value because the statement includes a recursion code ( ) that recalls the function. return return countDown(0) countDown Therefore, while retaining the other parts of the statement (that is, ), the computer will execute the recursion code ( ). So, the code will automatically invoke the function while passing in the value . return 2 + ", " + 1 + ", " + only countDown(0) countDown(0) countDown 0 Then, the function will start running again by checking if is less than . 0 0 Since is not less than , the computer skipped to the recursion code like so: 0 0 + + + + num + + countDown(num - ); + + + + + + countDown( - ); + + + + + + countDown( ); return 2 ", " 1 ", " ", " 1 return 2 ", " 1 ", " 0 ", " 0 1 return 2 ", " 1 ", " 0 ", " -1 Step 5: Execute only the recursion code Yet again, the command (in step 4) cannot return any value because the statement includes a recursion code ( ) recalling the function. return return countDown(-1) countDown Therefore, while retaining the other parts of the statement (that is, ), the computer will execute the recursion code ( ). So, the code will automatically invoke the function while passing in the value . return 2 + ", " + 1 + ", " + 0 + ", " + only countDown(-1) countDown(-1) countDown -1 Then, the function will start running again by checking if is less than . -1 0 At this point, is less than . Therefore, the computer will execute the code of the statement by returning the value like so: -1 0 if “Recursion Stopped!” + + + + + + ; return 2 ", " 1 ", " 0 ", " "Recursion Stopped!" At last, the statement now has values it can validly concatenate and return. Therefore, the returned value from the recursion function will be: return countDown "2, 1, 0, Recursion Stopped!" Conclusion All in all, a is a function that repeatedly recalls itself until something stops the recall. recursive function Previously published at - CodeSweetly