Signup/Sign In
PUBLISHED ON: FEBRUARY 6, 2023

JavaScript Program to Generate Fibonacci Sequence Using Recursion

The Fibonacci sequence is a well-known mathematical pattern that goes from 0 to 1 by adding the two numbers before it. Using recursion, a programming method in which a function calls itself over and over to solve a problem, the Fibonacci sequence can be shown in JavaScript.

In this article, we'll look at how to use recursion to make a JavaScript program that shows the Fibonacci sequence, including how to implement the recursive function and what the final result will be. By the end of this article, you will know more about how recursion works and how to use it to show the Fibonacci sequence in JavaScript. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

What is a Fibonacci Series?

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, starting from 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. This sequence was named after Leonardo of Pisa, an Italian mathematician, who introduced it in his book "Liber Abaci" published in 1202. The Fibonacci sequence has several interesting properties and appears in various areas of mathematics, science, and nature, making it an important concept to understand.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . . . 
Fn = Fn-1 + Fn-2 
F0 = 0 and F1 = 1.

Approach

A recursive function is utilized by Fibonacci() to find the Fibonacci sequence till a particular number.

The user is asked to prompt a number to which the function will print.

  • The if ..else .. statement is used to check if the number is greater than 0 or not.
  • If the number is greater than 0, a for loop is used to calculate each term recursively (calls the fibonacci() function again).

Program to Show Fibonacci Sequence Using Recursion

// JavaScript program to display Fibonacci sequence using recursion
function Fibonacci(num) {
    if(num < 2) 
        return num;
    
    else 
        return Fibonacci(num-1) + Fibonacci(num - 2);
}

var terms = prompt('Enter the number of terms: ');

if(terms <=0) 
    console.log('Enter a positive integer.');

else {
    for(let i = 0; i < terms; i++) 
        console.log(Fibonacci(i));   
}


Enter the number of terms: 7
0
1
1
2
3
5
8

Conclusion

In conclusion, the Fibonacci sequence is a well-known math pattern that can be shown using recursion in JavaScript. Setting up a recursive function that adds the two previous numbers in the sequence to get the next one, and then calling that function over and over again until the number of terms you want is reached, is how the process works.

To become a good JavaScript programmer, it's important to understand how to use recursion to solve problems like showing the Fibonacci sequence. By using this program, you will have a better understanding of the Fibonacci sequence and recursion in JavaScript.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.