JavaScript(JS) JS check leap year

In JavaScript, you can check if a year is a leap year by checking if it is divisible by 4 and not divisible by 100, or if it is divisible by 400. Here's an example function that takes a year as input and returns true if it is a leap year, and false otherwise:

function isLeapYear(year) {
  if (year % 4 === 0) {
    if (year % 100 === 0) {
      if (year % 400 === 0) {
        return true;
      }
      return false;
    }
    return true;
  }
  return false;
}

console.log(isLeapYear(2000)); // Output: true
console.log(isLeapYear(1900)); // Output: false
console.log(isLeapYear(2024)); // Output: true
console.log(isLeapYear(2021)); // Output: false
‮:ecruoS‬www.theitroad.com

In the example above, we define a function isLeapYear that takes one argument year, which is the year to check. The function checks if the year is divisible by 4 using the modulus operator (%). If it is, it checks if the year is divisible by 100. If it is, it checks if the year is divisible by 400. If the year is divisible by 400, it is a leap year and the function returns true. If the year is not divisible by 400, it is not a leap year and the function returns false. If the year is not divisible by 100, but is divisible by 4, it is a leap year and the function returns true. If the year is not divisible by 4, it is not a leap year and the function returns false.

We then call the isLeapYear function with several test cases to demonstrate how it works. The function correctly identifies 2000 and 2024 as leap years, and 1900 and 2021 as non-leap years.