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
JavaScript Conditional Statements
JavaScript Loops
Exercises & Assignments
Write a JavaScript program to check even or odd number
Write a JavaScript program to find the largest of three numbers
Write a JavaScript program to check leap year
Write a JavaScript program to print the multiplication table of a number
Write a JavaScript program to find the factorial of a number
Write a JavaScript program to print the Fibonacci series
Write a JavaScript program to check prime number
Interview Questions & Answers
What is the difference between for…in and for…of?