fetch range of years in javascript

To fetch a range of years in Javascript, you can use a combination of the Date object and an array to store the years. Here is an example code block that will generate an array of years from the current year back to 100 years ago:

index.tsx
let currentYear = new Date().getFullYear();
let years = [];

for (let i = 0; i <= 100; i++) {
  years.push(currentYear - i);
}

console.log(years);
148 chars
9 lines

This code block first gets the current year using the Date object's getFullYear() method. It then initializes an empty array to store the years. Finally, it runs a for loop to add the years to the array, subtracting the loop counter from the current year at each iteration.

You can adjust the loop range to fetch a different range of years.

gistlibby LogSnag