(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Recursion.
JavaScript Recursion
In this tutorial, you will learn about recursion in JavaScript with the help of examples.
Recursion is a process of calling itself. A function that calls itself is called a recursive function.
The syntax for recursive function is:
function recurse() {
// function code
recurse();
// function code
}
recurse();
Here, the recurse()
function is a recursive function. It is calling itself inside a function.
A recursive function must have a condition to stop calling itself. Otherwise, the function is called indefinitely.
Once a condition is met, the function stops calling itself. This is called a base condition.
To prevent infinite recursion, you can use if…else statement (or similar approach) where one branch makes the recursive call, and the other doesn’t.
So, it generally looks like this.
function recurse() {
if(condition) {
recurse();
}
else {
// stop calling recurse()
}
}
recurse();
A simple example of a recursive function would be to count down the value to 1.
Example 1: Print Numbers
// program to count down numbers to 1
function countDown(number) {
// display the number
console.log(number);
// decrease the number value
let newNumber = number - 1;
// base case
if (newNumber > 0) {
countDown(newNumber);
}
}
countDown(4);
Output
4 3 2 1
In the above program, the user passes a number as an argument when calling a function.
On each iteration, the number value is decreased by 1 and function countDown()
is called until the number is positive. Here, newNumber > 0
is the base condition.
This recursive call can be explained in the following steps:
countDown(4) prints 4 and calls countDown(3) countDown(3) prints 3 and calls countDown(2) countDown(2) prints 2 and calls countDown(1) countDown(1) prints 1 and calls countDown(0)
When the number reaches 0, the base condition is met, and the function is not called anymore.
Example 2: Find Factorial
// program to find the factorial of a number
function factorial(x) {
// if number is 0
if (x === 0) {
return 1;
}
// if number is positive
else {
return x * factorial(x - 1);
}
}
let num = 3;
// calling factorial() if num is non-negative
if (num > 0) {
let result = factorial(num);
console.log(`The factorial of ${num} is ${result}`);
}
Output
The factorial of 3 is 6
When you call function factorial()
with a positive integer, it will recursively call itself by decreasing the number.
This process continues until the number becomes 1. Then when the number reaches 0, 1 is returned.
This recursive call can be explained in the following steps:
factorial(3) returns 3 * factorial(2) factorial(2) returns 3 * 2 * factorial(1) factorial(1) returns 3 * 2 * 1 * factorial(0) factorial(0) returns 3 * 2 * 1 * 1
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.