Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Representation of the Fibonacci Series
3.
Steps to Find the Fibonacci Series of n Numbers
4.
Examples of Fibonacci Series in JavaScript
4.1.
Get Fibonacci Series up to N terms
4.2.
Get Fibonacci Series of the first 8 terms
4.3.
Get the Sum of the First 7 Terms of the Fibonacci Series
4.4.
Get the Fibonacci Series using the Recursion Function
4.5.
Get the Fibonacci Series in Reverse Order
5.
Frequently Asked Questions
5.1.
How to implement Fibonacci in JavaScript?
5.2.
What is the Fibonacci series used for?
5.3.
How do you use the Fibonacci algorithm?
5.4.
How many variables are needed to find Fibonacci series?
5.5.
How does Fibonacci sequence work in JavaScript?
5.6.
Which algorithm is widely used for Fibonacci series in JavaScript?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Fibonacci Series in JavaScript

Author Ankit Mishra
1 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

The Fibonacci series in JavaScript is the number sequence in which the given number results from adding the two previous numbers.

The sum of the two preceding numbers makes the Fibonacci series in JavaScript. The Fibonacci series' first two terms are 0 and 1, respectively. The two prior terms are added to create the successive terms.

Fibonacci Series in JavaScript

Representation of the Fibonacci Series

                                                            Fn = Fn-1 + Fn-2

The above expression is used to find the Fibonacci sequence in JavaScript. Here, Fn represents the addition of the previous terms (Fn - 1) and (Fn - 2). Here Fn-1 is the first term, and Fn-2 is the second term of the Fibonacci series. This simply means that just add the two numbers before the term we needed to find to get the Fibonacci term.

Let’s try to understand the Fibonacci series in JavaScript with an example:

Example:

Find the Fibonacci number when n = 6.

Solution:

The formula to calculate the Fibonacci Sequence is: Fn = Fn-1 + Fn-2

As we know, F0 = 0 and F1 = 1

Using the formula Fn = Fn-1 + Fn-2, we get

F2 = F1 + F0 = 1 + 0 = 1

F3 = F2 + F1 = 1 + 1 = 2

F4 = F3 + F2 = 2 + 1 = 3

F5 = F4 + F3 = 3 + 2 = 5

F6 = F5 + F4 = 5 + 3 = 8

Therefore, F6 is 8.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Steps to Find the Fibonacci Series of n Numbers

  • Step 1: Take the first elements of the sequence to be 0 and the second element as 1.
     
  • Step 2: Sum up the first and second elements; the result is our third element.
     
  • Step 3: Now take the second and third elements and add them; the result is our fourth element.
     
  • Step 4: Repeat this process till n numbers are generated.

 

In general, the nth number of the series is equal to the (n-1)th + (n-2)th number.

Examples of Fibonacci Series in JavaScript

A Fibonacci series in JavaScript is written as following:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 and so on.

As we have seen, the Fibonacci sequence is the integer sequence in which the first two terms are 0 and 1. After that, the following term is defined as the sum of the previous two terms. Having this piece of information, let's start to code the Fibonacci series in JavaScript.

Get Fibonacci Series up to N terms

Here in the below code, We have received the input in the variable N, which holds the value up to which we need to find the Fibonacci. Later we declared the first two known terms of the Fibonacci series, and then we iterated until the required number of terms weren't generated in the output. And then Constantly updating the fn1 and fn2 variables.

const N = parseInt(prompt('Enter the number of terms: '));
let fn1 = 0, fn2 = 1, nextFibonacci;

console.log('Fibonacci Series upto N terms is:');

for (let i = 1; i <= N; i++) {
   console.log(fn1);
   nextFibonacci = fn1 + fn2;
   fn1 = fn2;
   fn2 = nextFibonacci;
}


Output

Enter the number of terms: 6
Fibonacci Series up to N terms is:
0
1
1
2
3
5


The user is requested in the above code to input the number of terms they want in the Fibonacci series in JavaScript.

The for loop repeats until the condition of for loop is failed.

The value 0 is printed first. The value of the second term is then saved in variable fn1 in each iteration, while the total of the two preceding terms is stored in variable fn2.

Now as we understood how we code the Fibonacci till N terms, let's move on to the next section to check out another version of the question based on the Fibonacci series in JavaScript.

Get Fibonacci Series of the first 8 terms

Here in the below code, We declared the first two known terms of the Fibonacci series, and then we iterated until 8 numbers of the sequence were generated in the output. And then Constantly updating the fn1 and fn2 variables.

let fn1 = 0, fn2 = 1, nextFibonacci;

console.log('Fibonacci Series upto 8 terms is:');

for (let i = 1; i <= 8; i++) {
   console.log(fn1);
   nextFibonacci = fn1 + fn2;
   fn1 = fn2;
   fn2 = nextFibonacci;
}


Output

Fibonacci Series upto 8 terms is:
0
1
1
2
3
5
8
13

Get the Sum of the First 7 Terms of the Fibonacci Series

Here in the below code, We declared the first two known terms of the Fibonacci series and a sum variable which was initialized to zero. Then we iterated until 7 numbers of the sequence were generated. We always added the current variable of the series to sum and updated the fn1 and fn2 variables.

let fn1 = 0, fn2 = 1, nextFibonacci, sum = 0;

console.log('Sum of the first 7 terms of the Fibonacci series is:');

for (let i = 1; i <= 7; i++) {
   sum = sum + fn1;
   nextFibonacci = fn1 + fn2;
   fn1 = fn2;
   fn2 = nextFibonacci;
}

console.log(sum);


Output

Sum of the first 7 terms of the Fibonacci series is:
20

Get the Fibonacci Series using the Recursion Function

Here in the below code, We declared the first two known terms of the Fibonacci series, and then we iterated until the desired numbers of the sequence were generated in the output. During each iteration, we called the function to generate the Nth fibonacci number using a function call. 

During every function call, we made a call to generate the (n-1)th and (n-2)th number which recursively went on till we reached either the first or the second number of the series where we return the number itself.

function nthFibonacci(currNum) {
    if(currNum <= 1) {
        return currNum;
    }
    else {
        return nthFibonacci(currNum-1) + nthFibonacci(currNum - 2);
    }
}

const N = parseInt(prompt('Enter the number of terms: '));

let fn1 = 0, fn2 = 1, nextFibonacci;

console.log('Fibonacci Series upto N terms is:');

for (let i = 0; i < N; i++) {
    console.log(nthFibonacci(i));   
}


Output

Enter the number of terms: 10
Fibonacci Series upto N terms is:
0
1
1
2
3
5
8
13
21
34

Get the Fibonacci Series in Reverse Order

Here in the below code, We declared an array to store the series, We updated first and second variables of the array of 0 and 1 since they are the first two numbers in the sequence. We iterated till N numbers were generated and updated the current position in the array to the sum of last 2 positions.

In the end, we started from N-1 and iterated over the array in reverse order till the 0th index to print the fibonacci sequence in reverse order.

const N = parseInt(prompt('Enter the number of terms: '));

console.log('Fibonacci Series upto N terms in reverse order is:');

let a = [];

a[0] = 0;
a[1] = 1;


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

for (let i = N-1; i >= 0; --i) {
   console.log(a[i]);
}


Output

Fibonacci Series upto N terms in reverse order is:
13
8
5
3
2
1
1
0

Frequently Asked Questions

How to implement Fibonacci in JavaScript?

To implement Fibonacci in JavaScript, create a function that starts with two variables (0 and 1), then iteratively adds them while storing the previous two values in a loop, and returns the result.

What is the Fibonacci series used for?

The Fibonacci series is used in various fields like mathematics, computer science, and nature modeling to describe growth patterns, sequences, and algorithms, aiding in problem-solving and data analysis.

How do you use the Fibonacci algorithm?

To use the Fibonacci algorithm, you apply it in coding or calculations by calling a function that generates Fibonacci numbers to solve problems involving sequences, patterns, or mathematical relationships.

How many variables are needed to find Fibonacci series?

Generally, 3 variables are declared. 2 variables can be initialized as the first(value = 0) and the second term(value = 1) of the series and the 3rd variable can be used to keep track of the length of the Fibonacci sequence to be printed.

How does Fibonacci sequence work in JavaScript?

Just like in any other programming language, a Fibonacci series in JavaScript is numerical series that starts with two fixed numbers 0 and 1. All the following numbers of the series can be generated using the sum of the last two numbers.

Which algorithm is widely used for Fibonacci series in JavaScript?

Generally loops are used to generate Fibonacci series in javaScript. You can use a for loop or a while loop according to your convenience.

Conclusion

In this article, we have extensively discussed the problem Fibonacci series in JavaScript.

To learn more about Javascript, see the Importance of JavaScriptJavascript Interview Questions.

Also read: fibonacci series in c

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas! 

Previous article
Find the Second Highest Element in an Array
Next article
Implement Linear Search and Binary Search in an Array
Live masterclass