You have never declared fib to be an array. Use var fib = []; to solve this.

Also, you're never modifying the y variable, neither using it.

The code below makes more sense, plus, it doesn't create unused variables:

var i;
var fib = [0, 1]; // Initialize array!

for (i = 2; i <= 10; i++) {
  // Next fibonacci number = previous + one before previous
  // Translated to JavaScript:
  fib[i] = fib[i - 2] + fib[i - 1];
  console.log(fib[i]);
}

Answer from Rob W on Stack Overflow
🌐
Neptuneworld
neptuneworld.in › blog › generate-fibonacci-sequence-javascript-hackerank
Generate Fibonacci Sequence - JavaScript | Hackerank - Neptuneworld.in
Complete detailed information on Write a JavaScript function fibonacciSequence() to generate a FIbonacci sequence.
🌐
HackerRank
hackerrank.com › challenges › ctci-fibonacci-numbers › problem
Recursion: Fibonacci Numbers | HackerRank
The Fibonacci sequence to is . With zero-based indexing, . ... Complete the recursive function in the editor below.
🌐
Medium
medium.com › @mtuncusa › hacker-rank-fibonacci-numbers-solutions-in-javascript-4a743e0d3fae
Hacker Rank Fibonacci Numbers solutions in JavaScript | by Mustafa Tunc | Medium
July 20, 2022 - 8. Our second number in the temp was saved as first number for the next step and pushed this number to the fibonacci series array.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-recursion-function-exercise-6.php
JavaScript recursion function: Get the first n Fibonacci numbers - w3resource
var fibonacci_series = function (n) { // Base case: if n is less than or equal to 1, return the base series [0, 1]. if (n <= 1) { return [0, 1]; } else { // Recursive case: generate the Fibonacci series up to (n - 1). var s = fibonacci_series(n ...
🌐
YouTube
youtube.com › watch
How to Print Fibonacci Series in JavaScript | For Loop & Array - YouTube
How to Print Fibonacci Series in JavaScript,fibonacci series in javascript w3schools,write a program to fibonacci series in javascript using for loop,fibonac...
Published   September 2, 2021
🌐
HackerRank
hackerrank.com › challenges › js10-function › topics
Day 1: Functions Topics | 10 Days of Javascript Tutorial
Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
Find elsewhere
🌐
Medium
medium.com › @marktbss › js-hackerrank-even-fibonacci-numbers-b64f9160d240
[JS] HackerRank : Even Fibonacci numbers | by Aiya Aiyara | Medium
December 10, 2023 - n = 10 Fibonacci ทั้งหมดที่ไม่เกิน 10 1, 2, 3, 5, 8 โจทย์ต้องการเฉพาะเลขคู่ ซึ่งก็คือ 2, 8 แสดงผลรวมซึ่งก็คือ 10 ออกไป · มาเขียน Function สร้าง Fibonacci กันก่อน (n = 100)
🌐
Scribd
scribd.com › document › 589743729 › javascript-Hands-on
1.try-It-Out - Function For Fibonacci Series Welcome To To Generate Fibonacci Sequence New | PDF | Information Technology Management | Computer Data
javascript Hands-on - Free download ... contains 5 code snippets for JavaScript functions: 1. A function to generate the Fibonacci sequence up to a given input number....
🌐
Javatpoint
javatpoint.com › fibonacci-series-in-javascript
Fibonacci series in JavaScript - javatpoint
Fibonacci series in JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
🌐
Faisal Akbar
dsfaisal.com › articles › js › js-problem-solving
JavaScript frequently asked problem solving question with solution
November 2, 2020 - Since in fibonacci sequence any ... use for loop to create fibonacci series, in this problem solving exercise I will use recursive function to create fibonacci series....
🌐
CodePen
codepen.io › megkadams › pen › jmZOWv
JavaScript Fibonacci
An online code editor, learning environment, and community for front-end web development using HTML, CSS and JavaScript code snippets, projects, and web applications.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-print-fibonacci-series
JavaScript Program to print Fibonacci Series - GeeksforGeeks
The recursion approach generates the Fibonacci series by recursively summing the previous two terms, starting from the base cases 0 and 1. This method is simple but can be inefficient for large sequences without optimization. Example: In this example the function uses recursion to calculate the nth Fibonacci number by summing the results of fibonacci(num - 1) and fibonacci(num - 2), with base cases for 1 and 2.
Published   July 15, 2025
🌐
Medium
jacksongrowson.medium.com › solving-the-fibonacci-sequence-using-javascript-8ef9d3d4123b
Solving the Fibonacci Sequence Using JavaScript | by Jackson Beytebiere | Medium
February 19, 2021 - When the calls of the function returns something besides another call(the base case) and no other function calls are que’d, the function’s operation is complete. The tower of blocks is then added up by the computer and the sum of blocks returned. In the making of the iterative solution, I found the most simple cases to be any number less than 2. These cases of the 1th iteration and 2nd both return 1. ... As before, I checked if n is less than 2. Otherwise I returned the sum of the last 2 Fibonacci numbers.
🌐
YouTube
youtube.com › watch
JavaScript Algorithms - 7 - Fibonacci Sequence - YouTube
⚡️ Code with me on Replit - http://join.replit.com/codevolution⚡️ View and edit the source code on Replit - https://bit.ly/3PiRR7D📘 Courses - https://learn....
Published   May 22, 2022
🌐
Tetotartozekok
tetotartozekok.hu › ww-spies-zcsyy › article.php
function for fibonacci series in javascript hackerrank solution
Requirements See the Pen JavaScript ... Ask Question Asked 2 years, 7 months ago. 1+1=2 and so on. 0. This integer argument represents the position in Fibonacci series and returns the value at that position.Thus, if it receives 5, it returns the value at 5th position in Fibonacci ...