Menü schliessen
Created: October 20th 2021
Last updated: October 20th 2021
Categories: IT Development,  JavaScript Development
Author: Marcus Fleuti

JavaScript Basics: Fibonacci loops

Tags:  development,  fibonacci,  for,  Javascript,  js,  loop,  while
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

What is Fibonacci?

Fibonacci is an infinite sequence of numbers. It normally starts with [1, 1].

To calculate the fibonacci numbers you need to add the first number with the number following it.
So that means in the first step we are calculating 1 + 1 = 2
Now we have expanded our sequence of numbers and are now by [1, 1, 2].

To calculate further we need to add the last number to the number next-to-last.
Which means we are calculating now 1 + 2 = 3
Our sequence of number got expanded again and we are now by [1, 1, 2, 3]

We can calculate further but I would like to show you how to do this with loops in JavaScript, so read further.

Fibonacci with JavaScript

Fibonacci in a "for" loop

You can achieve the fibonacci sequence with different loops, but the for loop is by far the most effective.
The procedure inside of the different loops is always the same. Just the different loops are making perfomance differences.

function fibonacciForLoop(fibonacciArray) {
    // Calculate fibonacci for 100 times in a for-loop
    for(let i = 0; i < 100 - 2;i++) {
        let cache = fibonacciArray[i];
        let result = cache + fibonacciArray[i+1];
        fibonacciArray.push(result);
    }
    console.log(`Fibonacci with for loop: ${fibonacciArray}`);
}

Fibonacci in a "while" loop

The while loop is less effective than a for loop but you can achieve the same results.

function fibonacciWhileLoop(fibonacciArray) {
    let i = 0;
    while(i < 100 - 2) {
        let cache = fibonacciArray[i];
        let result = cache + fibonacciArray[i+1];
        // Push the new result into the array
        fibonacciArray.push(result);
        i++;
    }
    console.log(`Fibonacci with while loop: ${fibonacciArray}`);
}

Fibonacci in a "do while" loop

The do while loop is not often used anymore and is less effective than the while and the for loop. (So I suggest you to don't use this loop)

function fibonacciDoWhileLoop(fibonacciArray) {
    let i = 0;
    do {
        let cache = fibonacciArray[i];
        let result = cache + fibonacciArray[i+1];
        // Push the new result into the array
        fibonacciArray.push(result);
        i++;
    } while (i < 100 - 2);
    console.log(`Fibonacci with do while loop: ${fibonacciArray}`);
}

Source Files

You can download a simple project below which contains all loops that are creating the fibonacci sequence:
Fibonacci loops sample

Fibonacci Facts

12 Naturns-Ideen | heilige geometrie, muster in der natur, fraktale in der naturYou can see the fibonacci sequence in the nature. For example you can see it in flowers, shells, trees and a lot of more things in the nature. It can also be seen by animals.

The fibonacci sequence is often used in photography to get the perfect positioning of the model or landscape. When the picture is taken with use of the fibonacci sequence it attracts your eye a lot more.