CodeNewbie Community 🌱

Discussion on: What's the difference between boolean values and truthy and falsy values?

Collapse
 
djuber profile image
Daniel Uber

a value is said to be "truthy" when it follows the true side of an if block.
A "falsey" value follows the false/else side (or at least doesn't follow the true side if you don't have an else).

Every language has its own decisions about which those are (in some languages, only true and false are truthy or falsey, and using any other value as the test in an if statement is an error).

An example of truthiness in action would be this python snippet

if(5):
  print(5)
Enter fullscreen mode Exit fullscreen mode

Or in ruby

if 5
  5
end
Enter fullscreen mode Exit fullscreen mode

Both of these will show 5, even though neither value was "true".

For javascript, a good reference is MDN, but you'll definitely want to check each languages documentation for truth when you start using them.

Other languages might have different ideas about what's valid for if conditionals. Some treat 0 as falsy, some treat an empty array as falsy, some consider an empty string as falsy, some treat a pointer to nothing as falsy. Some only treat false as falsy, and everything else as truthy.