JavaScript is a powerful programming language that can be used to perform a wide variety of tasks. One of the most popular uses of JavaScript is to generate the Fibonacci series, a sequence of numbers in which each number is the sum of the two preceding ones. This tutorial will explain how to use JavaScript to generate the Fibonacci series and print it to the console.



The Fibonacci series typically goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

It starts with 0 and 1, and each subsequent number in the sequence is the sum of the previous two numbers. For example, the following number in the sequence after 0 and 1 is 0 + 1 = 1; The next number is 1 + 1 = 2, then the next number is 1+2 = 3, and so on.

Print the Fibonacci Series Using for Loop

Here is a JavaScript program that uses a for loop to print out the Fibonacci series up to a certain number:

Example:

function printFibonacci(n) {
  let a = 0, b = 1, c;
  console.log(a);
  console.log(b);
  for (let i = 2; i < n; i++) {
    c = a + b;
    console.log(c);
    a = b;
    b = c;
  }
}

printFibonacci(10); // prints the first 10 numbers in the Fibonacci series

In the program above, the printFibonacci() function accepts a single argument, n, representing the number of elements in the Fibonacci series to be printed. The function uses three variables, a, b, and c, to store the current, previous, and next numbers in the series, respectively.

The function starts by initializing a to 0 and b to 1, which are the first two numbers in the Fibonacci series. The function then uses a for loop to iterate through the series, starting at the 2nd number (since the first two numbers are already set).

On each iteration of the loop, the function calculates the next number in the series by adding a and b together, and stores the result in c. The function then logs c to the console using the console.log() method.

After logging the number, the function updates the values of a and b so that a becomes the current number and b becomes the next number in the series. This allows the loop to move to the next number in the series on the next iteration.

The for loop continues until the value of i is greater than or equal to n, at which point the function exits, and the Fibonacci series up to the nth number is printed on the console.

Print the Fibonacci Series Using Recursion

Here is a JavaScript program that uses recursion to print out the Fibonacci series up to a certain number:

Example:

function printFibonacci(n) {
  if (n <= 0) {
    return 0;
  } else if (n === 1) {
    console.log(0);
    return 1;
  } else {
    let fib = printFibonacci(n - 1) + printFibonacci(n - 2);
    console.log(fib);
    return fib;
  }
}

printFibonacci(10); // prints the first 10 numbers in the Fibonacci series

In the program above, the printFibonacci() function accepts a single argument, n, which represents the number of elements in the Fibonacci series to be printed.

The function uses recursion to calculate the nth element of the Fibonacci series. It starts by checking if n is less than or equal to 0; in that case, it returns 0. Else if n is equal to 1, it will print 0 and return 1. The else block is the recursive block where it calls the same function twice with the argument n-1 and n-2, respectively, and adds their return value to get the nth element and print it.

The recursion continues until the base case is reached, at which point the function exits, and the Fibonacci series up to the nth number is printed on the console.

You can call the function with different values of n to print different numbers of elements in the series.

It is important to note that this solution may be less performant than a loop, as it generates many function calls, especially when n is a large number.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram