Run

Fibonacci series in JavaScript

Print the first n Fibonacci numbers. Press Run to try it, then change the code and run it again.

main.js

function fibonacci(n) {
  const series = [0, 1];
  while (series.length < n) {
    series.push(series.at(-1) + series.at(-2));
  }
  return series.slice(0, n);
}

console.log(fibonacci(10).join(", "));
Output

Press Run to see the output.

More JavaScript examples

Open the full JavaScript online compiler →