Fibonacci series in javascript using for loop

(Last Updated On: 19/12/2023)

Embark on a journey into the enchanting world of the Fibonacci series. Picture it like a magical sequence of numbers that pops up in many places, especially in web development using JavaScript. In this blog, we’ll explore the power of JavaScript, a language that makes websites interactive, and discover how a clever tool called the for loop crafts the mesmerizing patterns of the Fibonacci series. Get ready for a delightful adventure in the world of coding!

Dive into Fibonacci series in C using recursion Now!

Fibonacci series in javascript using for loop-Code

Learn Fibonacci series in javascript using for loop for below given code:

      <title> Fibonacci series in javascript using for loop </title>
         // declaration of the variables  
         var n1 = 0,  n2 = 1, next_num, i;  
         var number = parseInt (prompt (" Enter the limit : "));  
         document.write( "Fibonacci Series: ");  
         for ( i = 1; i <= number; i++)  
         {  document.write (" <br> " +  n1); // print the n1  
            	next_num = n1 + n2; // sum of n1 and n2 into the next_num  
            	n1 = n2; // assign the n2 value into n2  
            	n2 = next_num; // assign the next_num into n2  

Explanation of the code:

This HTML document contains JavaScript code to generate the Fibonacci series using a for loop, check out the below: :

  • Variable Declaration: `n1` and `n2` are initialized to 0 and 1, respectively. `next_num` and `i` are also declared.
  • User Input: The user is prompted to enter the limit for the series using `prompt` and the value is stored in the variable `number`.
  • Loop Structure: A `for` loop is used to iterate from 1 to the user-defined `number`.
  • Fibonacci Logic: The series is generated by adding `n1` and `n2` to obtain `next_num`. Values are updated for `n1` and `n2` to progress the series.
  • Output: The generated Fibonacci series is printed on the web page using `document.write`.

This code effectively utilizes JavaScript and HTML to create a simple web page that generates and displays the Fibonacci series based on user input.

Know the Fibonacci Series in Python Using For Loop here!


Fibonacci Series:

Comparison with Other Methods

When generating the Fibonacci series in JavaScript, various methods, including recursion and iterative approaches, are available. A brief comparison sheds light on their unique characteristics.


Recursion, a powerful programming concept, involves a function calling itself to solve a problem. While elegant and mirroring the mathematical definition of Fibonacci, recursion in JavaScript may lead to stack overflow for large series due to its synchronous nature. This could impact performance and memory usage.

Iterative Methods (For Loop):

Using a for loop in JavaScript for Fibonacci series generation offers a straightforward and efficient alternative. The for loop is particularly advantageous in terms of simplicity, readability, and ease of implementation. It provides explicit control over initialization, condition, and iteration steps, making it well-suited for Fibonacci series, where each term relies on the previous ones.

Advantages of For Loop:

1. Readability: The for loop structure is concise and easy to understand, promoting code readability.

2. Control: Developers have explicit control over loop variables, making it easy to manage the series progression.

3. Efficiency: For loops generally result in more efficient code execution compared to certain recursive solutions, particularly for large series.


1. Fixed Iterations: For loops require a predetermined number of iterations, limiting flexibility.

2. Code Length: Depending on the context, recursive solutions might offer more concise code for smaller series.

In conclusion, the Fibonacci series in JavaScript using for loop, a captivating sequence, holds significance in web development. Unveiling its magic, the for loop emerges as a powerful ally, offering simplicity and efficiency. Mastering this tool not only enhances code readability but also propels the seamless generation of the Fibonacci series in the dynamic landscape of JavaScript programming.

Learn the code here and then try it on the Newtum Online Compiler! Just click, and start exploring the endless possibilities of coding. Learn, experiment, and execute your code effortlessly with Newtum – the perfect platform for coding enthusiasts!

Learn How to Generate Fibonacci series in C using for loop, Now!

FAQ for Fibonacci Series in Javascript

Can I use recursion to generate the Fibonacci series in JavaScript?

Yes, recursion is a valid approach, but for large series, it may lead to stack overflow. Consideration of system limits is crucial.

Is the for loop the only way to generate the Fibonacci series in JavaScript?

No, other methods like recursion or while loops can also be employed. The choice depends on coding preferences and specific requirements.

How can the Fibonacci series generated in JavaScript be applied in web development?

The fibonacci series can be used for animations, optimizing algorithms, and creating aesthetically pleasing design patterns in web development.

What happens if I enter a non-numeric value as the limit for the Fibonacci series in the provided code?

The code won’t work as expected. It’s crucial to handle user input validation to ensure the program runs smoothly.

Can I modify the code to start the Fibonacci series from different initial numbers in JavaScript?

Yes, by adjusting the initial values of n1 and n2 in the code, you can start the series from different numbers in JavaScript.

About The Author