3 ways in JavaScript to print the Fibonacci series

Write a JavaScript program to print the Fibonacci series:

The Fibonacci series is an infinite series of numbers where the first two numbers are 0, 1 and each number in the series is equal to the sum of the previous two numbers.

The first 10 numbers of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

The JavaScript program will take the value of n as input from the user and print the Fibonacci series up to the nth term.

How to print the Fibonacci series programmatically:

To print the Fibonacci series programmatically, the program will have to keep the previous two numbers in order to find the current number. We will use one loop to print the series.

The program will use the below algorithm to print the first n numbers of the Fibonacci series:

  • Take the value of n as input from the user.
  • Initialize two variables prev1 and prev2 as 0 and 1. These are the first two numbers of the series.
  • Run one for loop for n number of times.
  • Inside the loop,
    • Print the value of prev1.
    • Calculate the next value of the series by adding the two variables prev1 and prev2.
    • Assign the value of prev2 to prev1.
    • Assign the sum value to prev2.
  • The loop will print the first n Fibonacci series values one by one.

Example 1: JavaScript program to print the Fibonacci series by using a for loop:

Let’s write down the JavaScript program to print the Fibonacci series by using a for loop:

const n = 10;

console.log(`First ${n} numbers of the Fibonacci series are: `);

let prev1 = 0,
  prev2 = 1,
  sum;

for (let i = 0; i < n; i++) {
  console.log(prev1);
  sum = prev1 + prev2;
  prev1 = prev2;
  prev2 = sum;
}

Here,

  • n holds the numbers of the Fibonacci series to print.
  • The for loop runs from i = 0 to i = n - 1 and it prints the value of prev1.
  • On each iteration, the values of prev1 and prev2 are also updated with the new values.

If you run this program, it will print:

First 10 numbers of the Fibonacci series are: 
0
1
1
2
3
5
8
13
21
34

Example 2: JavaScript program to print the Fibonacci series by using a while loop:

Let’s use a while loop to write the same program. For a while loop, we can only check for the condition in the loop. The variable used in the loop should be initialized outside the loop. The end operation, i.e. increment of the variable can be done at the end of each iteration of the loop.

const n = 10;

console.log(`First ${n} numbers of the Fibonacci series are: `);

let prev1 = 0,
  prev2 = 1,
  i = 0,
  sum;

while (i < n) {
  console.log(prev1);
  sum = prev1 + prev2;
  prev1 = prev2;
  prev2 = sum;
  i++;
}

It will print similar output.

First 10 numbers of the Fibonacci series are: 
0
1
1
2
3
5
8
13
21
34

Example 3: Recursive function to print the Fibonacci series in JavaScript:

A recursive function calls itself directly or indirectly. We can use a recursive function to print the Fibonacci series in JavaScript. The function will build the array holding all Fibonacci series numbers.

function getFibonacci(n) {
  if (n === 1) {
    return [0];
  } else if (n === 2) {
    return [0, 1];
  } else {
    let series = getFibonacci(n - 1);
    series.push(series[series.length - 1] + series[series.length - 2]);
    return series;
  }
}

const n = 10;
let f = getFibonacci(n);

console.log(`First ${n} numbers of the Fibonacci series are: ${f}`);

Here,

  • The getFibonacci function is used to create the Fibonacci series.
  • It returns the final Fibonacci series array up to the nth number.
  • If the value of n is equal to 1 or 2, it returns an array holding the first number or the first two numbers. Else, it calls itself recursively to create the first n - 1 Fibonacci series. It appends the next number to the array by adding the last two numbers of the array. This array is returned to the caller function.

The above example will print the first 10 numbers of the Fibonacci series:

First 10 numbers of the Fibonacci series are: 0,1,1,2,3,5,8,13,21,34

You might also like: