0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Fibonacci Series

You can print the Fibonacci series in JavaScript by using a loop and two variables to store the last two numbers, like this: let a = 0, b = 1; for(let i = 0; i < n; i++) { console.log(a); [a, b] = [b, a + b]; }.
📋

Examples

Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
🧠

How to Think About It

To print the Fibonacci series, start with the first two numbers 0 and 1. Then, keep adding the last two numbers to get the next one. Repeat this process until you have printed the desired count of numbers.
📐

Algorithm

1
Get the number of terms to print (n).
2
Initialize two variables with 0 and 1 for the first two Fibonacci numbers.
3
Use a loop to repeat n times.
4
In each loop, print the current number.
5
Update the two variables to the next two Fibonacci numbers by adding them.
6
End the loop after printing n numbers.
💻

Code

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

printFibonacci(5);
Output
0 1 1 2 3
🔍

Dry Run

Let's trace printing 5 Fibonacci numbers through the code

1

Initialize

a = 0, b = 1

2

Iteration 1

Print a=0, update a=1, b=1

3

Iteration 2

Print a=1, update a=1, b=2

4

Iteration 3

Print a=1, update a=2, b=3

5

Iteration 4

Print a=2, update a=3, b=5

6

Iteration 5

Print a=3, update a=5, b=8

Iterationa (printed)b (next)
101
211
312
423
535
💡

Why This Works

Step 1: Start with first two numbers

We begin with a = 0 and b = 1 because the Fibonacci series starts with 0 and 1.

Step 2: Print current number

In each loop, we print the current value of a which holds the current Fibonacci number.

Step 3: Update numbers for next iteration

We update a and b to the next two Fibonacci numbers by setting a = b and b = a + b (using the old values).

🔄

Alternative Approaches

Recursive function
javascript
function fibonacci(n) {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

for (let i = 0; i < 5; i++) {
  console.log(fibonacci(i));
}
This method is simple but inefficient for large n because it recalculates values many times.
Using an array to store series
javascript
function printFibonacciArray(n) {
  const fib = [0, 1];
  for (let i = 2; i < n; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
  }
  for (let i = 0; i < n; i++) {
    console.log(fib[i]);
  }
}

printFibonacciArray(5);
This method stores all Fibonacci numbers, useful if you need to reuse them later but uses more memory.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs n times, so the time grows linearly with n.

Space Complexity

Only a few variables are used, so space stays constant regardless of n.

Which Approach is Fastest?

The iterative method is fastest and uses least memory compared to recursion or array storage.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Fast and memory efficient
Recursive functionO(2^n)O(n)Simple but slow for large n
Array storageO(n)O(n)When you need to reuse the series
💡
Use destructuring assignment [a, b] = [b, a + b] to update Fibonacci numbers cleanly.
⚠️
Beginners often forget to update both numbers simultaneously, causing incorrect series.