Never understood this if(variable)

So, I have seen this before, but never used it because ive never really understood it
for example in code pen will log 5 with the code below

var a = 5;
if(a){
  console.log(a);
}else{
  console.log("Not a")
}

is it like (while)? while is always going to be true, so thats why you would need a break if you use it. So a is 5 and true?

if (a) is always true because ‘5’ is a ‘truthy’. That is 5 evaluates to ‘true’.
There are values that are ‘falsey’ on the other hand like 0 which always evaluates to false.

1 Like

I will have to look that up. I have never heard of “truthy” or “falsey” before.

Most of it can be summarized as:
Falsey: 0, null, undefined, empty string (""), false, NaN.
Rest of what you can think of is truthy.

This is my understanding but there’s definetly something I’ve missed since it’s just what I can recover from memory, hope it helps.

1 Like

Definitely helps Always thought about it, but never asked. Now I can put it into practice

1 Like