How to check if a given year is a leap year in JavaScript?

One way to check if a given year is a leap year in JavaScript is by using the following logic:

function isLeapYear(year) { // If the year is divisible by 4 if (year % 4 === 0) { // If the year is divisible by 100, it is a leap year only if it is also divisible by 400 if (year % 100 === 0) { return year % 400 === 0; } return true; } return false; } // Example usage console.log(isLeapYear(2020)); // true console.log(isLeapYear(2021)); // false console.log(isLeapYear(2000)); // true console.log(isLeapYear(1900)); // false

The logic checks if the year is divisible by 4. If it is, it further checks if the year is divisible by 100. If it is divisible by 100, it is a leap year only if it is also divisible by 400. If it is not divisible by 100, it is a leap year.