Range of Years in JavaScript for a select box

JavaScript does have a Range object, but it refers to an arbitrary portion of the DOM and is not supported in IE 6/7.

If you want, you can simplify your function to this, but it’s all the same really.

this.years = function(startYear) {
    var currentYear = new Date().getFullYear(), years = [];
    startYear = startYear || 1980;  
    while ( startYear <= currentYear ) {
        years.push(startYear++);
    }   
    return years;
}
 
console.log( this.years(2019-20));

Leave a Comment