Fibonacci Series in JavaScript: Explanation & Code Examples

Introduction

The Fibonacci series is a famous mathematical sequence where each number is the sum of the two preceding numbers. It is widely used in algorithms, programming challenges, and real-world applications like cryptography and computer graphics.

In this blog, we’ll explore:
βœ… What the Fibonacci series is
βœ… How to generate it in JavaScript using loops and recursion
βœ… Performance considerations for different implementations


What is the Fibonacci Series?

The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two numbers.

πŸ”’ Fibonacci Series Example:

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

Mathematically, it is defined as:
F(n) = F(n-1) + F(n-2)
where:

  • F(0) = 0

  • F(1) = 1

Now, let's see how to implement this in JavaScript.


Method 1: Using a Loop (Iterative Approach)

The iterative approach is efficient in terms of time complexity (O(n)) and avoids recursion overhead.

function fibonacciSeries(n) {
    let fib = [0, 1];

    for (let i = 2; i < n; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }

    return fib;
}

// Example Usage
console.log(fibonacciSeries(10));  
// Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Explanation:

  1. We initialize an array with [0, 1].

  2. We use a for loop to compute each Fibonacci number by summing the previous two numbers.

  3. The function returns an array of the first n Fibonacci numbers.

βœ… Pros: Fast and memory-efficient.
❌ Cons: Requires storing all numbers in an array.

Method 2: Using Recursion (NaΓ―ve Approach)

The recursive method follows the mathematical definition but is inefficient due to repeated calculations.

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

// Example Usage
console.log(fibonacci(10));  
// Output: 55 (10th Fibonacci number)

Explanation:

  • Base case: If n is 0 or 1, return n.

  • Recursive case: Compute fibonacci(n-1) + fibonacci(n-2).

❌ Cons: This approach is very slow (O(2^n) time complexity) due to redundant calculations.

Method 3: Optimized Recursion with Memoization

We can optimize the recursive approach using memoization to store previously computed values.

function fibonacciMemo(n, memo = {}) {
    if (n in memo) return memo[n];
    if (n <= 1) return n;

    memo[n] = fibonacciMemo(n - 1, memo) + fibonacciMemo(n - 2, memo);
    return memo[n];
}

// Example Usage
console.log(fibonacciMemo(10));  
// Output: 55

Why is this better?

By storing already computed Fibonacci numbers, we reduce time complexity from O(2^n) to O(n).

Method 4: Using the Golden Ratio Formula

Method 4: Using the Golden Ratio Formula

A mathematical approach based on Binet's formula can compute Fibonacci numbers in constant time O(1).

function fibonacciFormula(n) {
    const sqrt5 = Math.sqrt(5);
    const phi = (1 + sqrt5) / 2;
    return Math.round(Math.pow(phi, n) / sqrt5);
}

// Example Usage
console.log(fibonacciFormula(10));  
// Output: 55

Why use this method?

βœ… Fastest approach (O(1) time complexity)
❌ Not ideal for generating a series, only for finding individual Fibonacci numbers.

Conclusion

In this blog, we explored different ways to implement the Fibonacci series in JavaScript.
πŸ“Œ For generating a series β†’ Use the iterative method.
πŸ“Œ For finding a single Fibonacci number β†’ Use Binet's formula.
πŸ“Œ For optimized recursion β†’ Use memoization.

πŸ‘‰ Try implementing these methods and experiment with larger values to compare performance! πŸš€