Write a JavaScript program to print the Fibonacci series

Description: Print the first 10 numbers of the Fibonacci series.

Hint:

  • Each number is the sum of the two before it.
  • Start with 0 and 1.

JavaScript Fibonacci program

let a = 0, b = 1;
const series = [];

for (let i = 0; i < 10; i++) {
  series.push(a);
  [a, b] = [b, a + b];
}

console.log(series.join(", "));

Output:

Tutorials Class - Output Window

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

How it works

  • [a, b] = [b, a + b] moves to the next pair.
  • join(", ") makes one text from the array.

Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.