Coding: Find the nth Fibonacci Number in JavaScript

Monu Kumar Modi
The Fresh Writes
Published in
4 min readFeb 16, 2023

--

In this article, we will be discussing a code snippet in which we will find the nth Fibonacci number.

Fibonacci Series: The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, and the subsequent numbers are calculated as the sum of the previous two.

Method 1: Using For loop

nth Fibonacci number

Let’s go through the code line by line to understand how it works:

function fibonacci(n) {

This line defines a function named fibonacci which takes an integer parameter n. The function will return the nth number in the Fibonacci sequence.

let a = 0;
let b = 1;

Here, we define two variables a and b, which represent the first two numbers in the sequence.

if (n === 0) {
return a;
}

If the input parameter n is equal to zero, we simply return the first number in the sequence, which is 0.

for (let i = 2; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}

This loop calculates the nth number in the sequence by iterating from the third number (i.e., i=2) up to the nth number. In each iteration, we calculate the sum of the previous two numbers (a and b) and store the result in a new variable c. We then update the value of a and b to the values of b and c, respectively.

return b;

After the loop is completed, we return the value of b, which represents the nth number in the sequence.

console.log(fibonacci(3));

Finally, we call the fibonacci function with an input parameter of 3, which should return the 3rd number in the Fibonacci sequence. The result of the function call is then printed to the console using the console.log function.

Complete Code:

function fibonacci(n) {
let a = 0;
let b = 1;

if (n === 0) {
return a;
}

for (let i = 2; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}

return b;
}

console.log(fibonacci(3));

Overall, the function uses a simple iterative approach to calculate the nth number in the Fibonacci sequence. This implementation is more efficient than a recursive approach, as it avoids the overhead of function calls and stack frames.

Method 1: Using Recursion

Let’s go through the code line by line to understand how it works:

function fibonacci(n) {

This line defines a function named fibonacci which takes an integer parameter n. The function will return the nth number in the Fibonacci sequence.

if (n <= 1) {
return n;
}

This is the base case of the recursive function. If the input parameter n is less than or equal to 1, we simply return the value of n. This is because the first two numbers in the Fibonacci sequence are 0 and 1, and any value less than or equal to 1 will return the same value.

return fibonacci(n - 1) + fibonacci(n - 2);

This is the recursive case of the function. If the input parameter n is greater than 1, we recursively call the fibonacci function with two input parameters: n-1 and n-2. We then return the sum of these two function calls, which is the nth number in the Fibonacci sequence.

console.log(fibonacci(2));

Finally, we call the fibonacci function with an input parameter of 2, which should return the 2nd number in the Fibonacci sequence. The result of the function call is then printed to the console using the console.log function.

Complete Code:

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

console.log(fibonacci(2));

Overall, the function uses a simple recursive approach to calculate the nth number in the Fibonacci sequence. However, this implementation has a time complexity of O(2^n) and can become very slow for large values of n. This is because the function recursively calls itself twice for each value, leading to an exponential increase in the number of function calls. Therefore, it is not recommended to use this implementation for large values of n.

--

--