Intelligence Center
Search
⌃K

Booleans

Understanding truthy, and falsy values
Imagine a light switch.
It’s either turned on or it’s turned off.
Pretty simple. Same thing with a checkbox. It's either ticked or not.
In programming, we have something similar that's called a boolean.
A boolean is a data type that has two possible values: true and false similar to our light switch, or our checkbox.
In a boolean context, like a conditional statement, and value will be either true or false. But how do we know which values will evaluate to true, and which to false? Introducing truthy, and falsy values!

Falsy Values

A falsy value is something that is not exactly false, but it's close enough to be considered false.
For example, if I ask you if you have any pets, and you say "no," then that's a falsy value because it's not exactly true or false, but it's close enough to be considered false.
Now, in programming, there are certain values that are always considered falsy. These include:
  • “” — an empty string (which is like saying "I don't have any pets")
  • 0 — the number 0 (which is like saying "I have zero pets")
  • null — the value "null" (which is like saying "I don't even know what pets are")
  • false — the boolean
  • undefined — Undefined means that the variable has been created, but has never been given a value.
  • NaN — Not a Number
If we put any of the abovementioned values in a boolean context, like a conditional statement, they will all evaluate to false.
As you can see, all of these values have some sort of emptiness to them.

Truthy Values

Anything that is not one of these falsy values is considered truthy.
For example, if you write the word "Hello" or "No", that's a truthy value because it's not one of the falsy values we just talked about.
Same with any number that’s not 0 — 1,5,-5,259 will all evaluate to true