Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialM Liu
105 PointsThe video about JavaScript was very confusing about Falseys and truthys. Is there any way to make it less confusing?
Can you explain Booleans in a clearer method?
3 Answers
A X
12,842 PointsHey M., From my understanding, True and False are Boolean values that are expressly defined in your code as such, whereas Truthy/Falsey are values that JavaScript has a pre-determined value on unless you expressly say otherwise. A value must be either True or False, so Truthy/Falsey is a way for JavaScript to assign a value to something so it doesn't (I assume) crash the program for things that aren't expressly said. To give a real world example from a well known coffee house that I used to work for:
- If someone comes up and says "I want a Grande Dark Roast w/ Cream" that would be an expressly defined because at this coffee house that's a menu item that be pre-defined characteristics: 16 oz cup, 3 oz of cream, and pour the dark roast coffee of the day till full, and put a lid and sleeve on it...so in code: Grande Dark Roast w/ Cream = TRUE
-If someone comes up to that same coffee house and said, "I want a regular cup of coffee" there's no such thing as a regular cup of coffee there, so this would be "Falsey" because I'd have to respond, "I don't know what you're talking about, we don't have a regular cup of coffee here. Please elaborate." So by default I have to respond FALSE because I have no clue what "regular" coffee means to this person, but no where in our said coffee house rules does it state what to do or say with someone that says "regular coffee". Regular Coffee (defaults to) = FALSE
-If the said coffee company had a company policy that you refuse business to anyone that says "I want a regular cup of coffee" that would be an expressly defined FALSE as the customer didn't say a menu item that's defined by the coffee chain. So I could say "I refuse to serve you." Regular Coffee = FALSE
-If someone came into the coffee shop (that only sold coffee), and asked for a sub, that would be NULL because the coffee house doesn't sell subs and would default to FALSE (falsey). So NULL (defaults to) = FALSE.
So a human brain has to think quickly to come up with an answer to all these situations, but in the end we have to have an answer for our customer (if we want to keep our job!), even if our "value" we need to default to is saying "I don't know"...and reduced to computer terms, that answer is either TRUE or FALSE. It can't be both, and it can't be neither. One or the other.
Here's an article to look at for more information on truthy/falsey: http://www.sitepoint.com/javascript-truthy-falsy/
Nathan Heffley
19,878 PointsHopefully this is what you're looking for:
A value is truthy when it is not false/null/empty.
A value is falsy when it is false/null/empty.
For example:
var number = 0
var bool = false
var string = "" // An empty string
those are all falsy values. They are nothing, therefore they are falsy.
var number = 17
var bool = true
var string = "some value"
these are all truthy values. They are something, therefore they are truthy.
M Liu
105 PointsThanks so much for the clarification!
Nathan Heffley
19,878 PointsYou're welcome!
Nathan Heffley
19,878 PointsIf this is the answer you were looking for, please mark it as the best answer so other students can see that it is correct.
A X
12,842 PointsA X
12,842 PointsAlso, I can't help but notice your low points score. Truthyness and Falseyness are more advanced skills. Unless you're a more advanced student, I'd recommend taking the Full Stack JavaScript Track first. The teacher, Dave McFarland, really breaks this stuff down. He starts talking about Boolean logic here, if you specifically want to look at basic Boolean: https://teamtreehouse.com/library/javascript-basics/making-decisions-with-conditional-statements/boolean-values
nfs
35,526 Pointsnfs
35,526 PointsGreat Explanation, nekilof ...