Fibonacci series in JavaScript17 Mar 2025 | 6 min read This section will discuss the Fibonacci series and how we can generate the Fibonacci series in JavaScript. ![]() Fibonacci series is a series that generates subsequent series of numbers by the addition of the two previous numbers. The first two terms of the Fibonacci series are zero and one, respectively. And the next terms are the addition of the two previous terms. Representation of the Fibonacci seriesFn = (Fn -1) + (Fn - 2) Fn represents the addition of the previous terms (Fn - 1) and (Fn - 2). Here Fn-1 is the first terms, and Fn-2 is the second terms of the Fibonacci series. Example:
Here is generated series: 0, 1, 1, 2, 3, … Similarly, we can find the series of the next terms. Steps to find the Fibonacci series of n numbersFollowing are the steps to find the series of the Fibonacci Series: Step 1: Declare the variables x, y, z, n, i Step 2: Initialize the local variable x = 1, y = 1, i = 2 Step 3: Read a number from the user Step 4: Display the value of x and y Step 5: Repeat the process of Fibonacci series until i > n
Step 6: Stop the execution of the Fibonacci series Get the Fibonacci series up to n termsLet's consider an example to get the Fibonacci series up to a desired numbers in JavaScript using for loop. Program1.html Output: When we execute the above program, it displays the given image. There is a prompt box to define the Fibonacci series limits, and then click the OK button to continue. ![]() After that, it displays the Fibonacci series that starts with 0 and 1. And the next term is the addition of its previous two terms, as shown below. ![]() Get the Fibonacci series of the first 8 termsLet's consider an example to get the Fibonacci series of the first 8 terms in JavaScript using for and if statements. Program3.html Output: ![]() Get the sum of the first 7 terms of the Fibonacci seriesLet's consider an example to get the sum of the Fibonacci series in JavaScript using function and for loop. Simple2.html Output When the above code is executed, it displays a prompt box that takes a number to return the sum of the Fibonacci series. ![]() Here we have entered 7 as the input to return the series sum, as shown below. ![]() Click on the OK button. ![]() In the above image, it returns the sum of first 7 terms is 21. Get the Fibonacci series using Recursion FunctionLet's consider an example to get the Fibonacci series in JavaScript using recursive function. Recursion.html Output ![]() In the above program, we have created the Fibonacci series using the recursion function that avoids the loop to display the series. The recursion function continuously calls the recur() function to print the series till the recur(12) is satisfied. At each iteration, the value of recur() function is decremented by 1 and store the value into the total variable. Get the Fibonacci series in Reverse OrderLet's consider an example to get the Fibonacci series in Reverse Order using for loop. Reverse.html Output When the above code is executed, it shows a prompt box to take a number from the user. ![]() Here we have entered 10 as the input and then click the OK button. After that, it generates the Fibonacci series of the first 10 terms in ascending order and in reverse order. ![]() Next TopicJavaScript appendchild() method |
JavaScript is one of the most renowned and vastly used programming languages. It is growing faster than any other language, and large companies like Netflix, PayPal, and Walmart build internal applications around JavaScript. It is an amazing opportunity to get a fantastic job by learning...
11 min read
The mouseup event works with the mouse movement on the web page using the javascript functionality. The mouse-up event works on the laptop's mouse, or a single mouse click using the mouseEvent event. If we release the press button on the laptop, then the mouse-up...
4 min read
When a user tries to load a page but it is unsuccessful, the page remains unloaded, and a JavaScript onunload event is triggered. The possibility exists that the onunload event will also happen if the browsers have been closed while the page is loaded. When a...
3 min read
What is "Vanilla JavaScript"? The term vanilla script is used to refer to the pure JavaScript (or we can say plain JavaScript) without any type of additional library. Sometimes people often used it as a joke. Nowadays several things can also be done without using any...
3 min read
JavaScript provides in-built functions to check whether the array is empty or not. Following are the method offered by JavaScript programming to check an empty array: length isArray(array) The Array.isArray() function checks the array type (passed parameter is an array or not) and array.length find the length of...
6 min read
? JavaScript, also known as JS, is one of the scripting (client-side scripting) languages, that is usually used in web development to create modern and interactive web-pages. The term "script" is used to refer to the languages that are not standalone in nature and here it...
4 min read
The ECMAScript6 (ES6) provides the string repeat() feature to repeat the given string. The string can provide data to display the required time with function and event. The repeat method does not affect the original data, but displays require time the similar data. In other words,...
4 min read
? Factorial of number is the product of all positive descending integers. Factorial of n is denoted by n!. For example - 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 Here, 4! is...
3 min read
? As we all know, the variable declaration is one of the basic and essential aspects of any programming language such as C language, C++, etc. However, JavaScript has a small odd, known as Hoisting, which can turn a flawless-looking declaration into a subtle bug. Hoisting In JavaScript,...
3 min read
When the mouse cursor passes on the HTML element, the onmouseover event is worked and shows user-defined functionality. This function also works when the mouse cursor selects the user element, html tag, or required data. When a user moves the cursor away from an element, the...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India