Better way to write a Leap Year Program in Javascript

Bah - screw efficiency. I prefer it when the code is much easier to understand. A few milliseconds won’t be missed.

Set the date to the 29th of February. If the date remains the 29th then it’s a leap year. Simple as that.

function isLeapYear(year) {
    var leapDay = new Date(year + "-02-29");
    return leapDay.getDate() === 29;
}
1 Like