JavaScript Program to Print the Fibonacci sequence

In this example, you will learn to program a Fibonacci sequence in JavaScript.

A fibonacci sequence is written as:

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms.


Example 1: Fibonacci Series up to n terms

// program to generate fibonacci series up to n terms

// take input from the user
let number = parseInt(prompt('Enter the number of terms: '));
let n1 = 0, n2 = 1, nextTerm;

console.log('Fibonacci Series:');

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

Output

Enter the number of terms: 4
Fibonacci Series:
0
1
1
2

In the above program, the user is prompted to enter the numbers of terms that they want in 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 of the second term is stored in variable n1 and the sum of two previous terms is stored in variable n2.


Example 2: Fibonacci Sequence Up to a Certain Number

// program to generate fibonacci series up to a certain number

// take input from the user
let number = parseInt(prompt('Enter a positive number: '));
let n1 = 0, n2 = 1, nextTerm;

console.log('Fibonacci Series:');
console.log(n1); // print 0
console.log(n2); // print 1

nextTerm = n1 + n2;

while (nextTerm <= number) {

    // print the next term
    console.log(nextTerm);

    n1 = n2;
    n2 = nextTerm;
    nextTerm = n1 + n2;
}

Output

Enter a positive number: 5
Fibonacci Series:
0
1
1
2
3
5

In the above example, the user is prompted to enter a number up to which they want to print the Fibonacci series.

The first two terms 0 and 1 are displayed beforehand. Then, a while loop is used to iterate over the terms to find the Fibonacci series up to the number entered by the user.