# Truthy and Falsy

Hello everyone,
I wrote a program to calculate leap years to practice JavaScript statements using if…else. But now I’m confused because I do not know how I can solve it for myself!
I wrote it in two ways and both ways I got the right answer. The first way is simple, but the second way I have no explanation about it and I am confused about it. Especially in the Truthy and Falsy part !!! it is kind of unlike calculation !

if ( !(year % 4) ){
if (!(year % 100)){
if ( !(year % 400)){

I would appreciate it if you guide me to understand it well.

/*----------------- Leap Year -------------- */
function isLeap (year) {
if (year % 4 === 0) {
if (year % 100 === 0){
if (year % 400 === 0){
`The year \${year} IS a leap year!!`;
} else {
return `The year \${year} is NOT a leap year!!`;
}
} else {
return `The year \${year} IS a leap year!!`;
}
}else {
return `The year \${year} is NOT a leap year!!`;
}
}

/*----------------- SECOND WAY -------------- */

const isLeap = year => {
if ( !(year % 4) ){
if (!(year % 100)){
if ( !(year % 400)){
console.log(`The year \${year} IS a leap year`);
}else {
console.log(`The year \${year} is NOT a leap year`);
}
}else {
console.log(`The year \${year} IS a leap year`);
}
}else {
console.log(`The year \${year} is NOT a leap year`);
}
};

isLeap(2024);
/*----------------- Leap Year End -------------- */

The second way is having a falsy value generated. You know that any leap year when divided with 4 will give a 0 as remainder, right?

Since, 0 is a falsy value, what is the negation of a falsy value? A truthy value, right?

`year%4` would result in a 0. And then, when you negate it, you get,

`!(year%4)` which would be a truthy value and it’d return a truthy value. And that’s why the if conditional is functioning correctly.

2 Likes

if “(year % 4) " is true then do " console.log( `The year \${year} IS a leap year` )” .And I know for example 2024 is dividable by 4. So normally the condition is true and `The year 2024 IS a leap year` would be printed so far.

but I reversed (year % 4) by adding an !. e.g !(year%4) that means if it isn’t true then " console.log( `The year \${year} IS a leap year` )". but it still true !.
that is why I am confused !

1 Like

To better understand the solution, check the following:
The first condition is

`year%4 === 0` which evaluates to true since leap year when divided by 4 return 0 as remainder which is equal to 0 (===0)
And hence the equation gets evaluated to truthy value.

However, the second condition is

`year%4` which means what is the remainder when the year is divided by 4. It is not a comparison; it takes the remainder as the answer. And every time there is a leap year, the answer is 0 which is a falsy value.

Now, you take this 0 as answer and negate it using `!(0)` and this would give a truthy value as answer since negation of falsy value is a truthy value.
Assume that the year is not a leap year. In that case, the answer of `year % 4` will be a positive number less than 4 which is a truthy value. And if you negate a truthy value, you’ll receive a falsy value.

To sum it up, first condition is a comparison between the remainder upon division and 0.
And the second condition is the negation of remainder which you get when you divide the year with 4.

I hope that clears up your question.

Another explanation:
Alternatively, what you can try to understand this is as follows:

``````let value1 = (2020 % 4 === 0);
let value2 = 2020 % 4;
console.log(value1);
console.log(value2);

console.log(!(value2)); // try negating the value of variable value2
``````

Observe what you get as value 1 and value 2.

1 Like

What is the criteria that determines a leap year? Every fourth year is a leap year if the year is divisible by 4. Why? Because a non-leap year is 365 and a quarter days long. The leap year makes up the missing day every four years.

But, there is still some disagreement over the long haul so centuries are excluded, albeit every fourth century is not if it is divisible by 400.

To my mind the first thing to rule out is the centuries.

``````year is divisible by 400?  return true
year is divisible by 100?  return false
year is divisible by 4?  return true
``````
``````const isLeapYear = function (x) {
return x % 400 === 0 ? true :
x % 100 === 0 ? false :
x % 4 === 0 ? true : false
}
print = console.log
print (isLeapYear(2000))
print (isLeapYear(2020))
print (isLeapYear(2021))
print (isLeapYear(2100))
``````
``````true
true
false
false
``````
3 Likes