Signup/Sign In
PUBLISHED ON: MARCH 14, 2023

JavaScript Program to Print the Fibonacci Number Sequence

For centuries, mathematicians, scientists, and laypeople alike have been fascinated by the Fibonacci sequence. It is a number sequence in which each number is the sum of the two preceding ones, usually beginning with 0 and 1. We will use JavaScript in this programme to print the Fibonacci sequence of a given length.

The Fibonacci sequence has numerous applications in mathematics, computer science, and nature, including research into the Golden Ratio, which appears in many natural phenomena such as spiral patterns in seashells and pinecones. The Fibonacci sequence and its relationship are used in various algorithms and dynamic programming in computer science. It is also used as a performance benchmark in computer languages.

The Fibonacci sequence is an intriguing topic to investigate, and by printing it with JavaScript, we can gain a better understanding of both the sequence and the capabilities of the JavaScript programming language. .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 sequence is a type series where each number is the sum of the two that precede it.

It starts from 0 and 1 usually. And the next terms are the addition of the two previous terms.

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

Approach:

In the program given below, the user is asked to send in the number of terms they want to have for the Fibonacci Series.

The for loop iterates up to the number entered by the user.

0 is printed at first. Then, in each iteration, the value is shifted to the next variables as in, the second term is stored in variable n1 and the sum of the previous two terms is stored in variable n2.

Program to Print the Fibonacci Number Sequence upto n terms

// JavaScript program to generate Fibonacci series up to n 

const num = parseInt(prompt('Enter the number of terms: '));
var n1 = 0, n2 = 1;

console.log('Fibonacci Series of first n terms are as follows:');

for (var i = 1; i < num+1; i++) {
    console.log(n1);
    var nextTerm = n1 + n2;
    n1 = n2;
    n2 = nextTerm;
}


Enter the number of terms: 6
Fibonacci Series of the first n terms are as follows:
0
1
1
2
3
5

Conclusion

To summarize, the Fibonacci sequence is an intriguing mathematical concept with numerous applications in various fields. By printing the sequence in JavaScript, we can not only gain a better understanding of the sequence but also learn more about the JavaScript programming language's capabilities.

This program shows how to generate and print a Fibonacci sequence of a given length, and it can be used to explore and experiment with the Fibonacci sequence. It can also be used as a starting point for more advanced programs that make use of the Fibonacci sequence in a variety of ways, such as algorithms and dynamic programming. Overall, the program can assist developers in developing an understanding of how to implement the concept in their programming.



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.