CodeNewbie Community


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

kingsofleon profile image Leon ・1 min read

Hi guys!

I'm new to coding but absolutely loving the journey so far!

I recently came across the concept of truthy and falsy values. Can anyone explain these and how they differ from booleans?

Thanks much!

Discussion (2)

anmpog profile image
Anthony Pogliano

Unless my understanding is incorrect (very possible) a Boolean in JavaScript is a primitive value of either true or false, while there are other values that evaluate to true or false when using a comparison operations. Primitive values are like the “building block” values - a string, a number, a Boolean, null, undefined, symbols, and bigints.

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

Enter fullscreen mode Exit fullscreen mode

Or in ruby

if 5
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.

Forem Open with the Forem app