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 trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Combining Multiple Tests Into a Single Condition

Non-Boolean Variable Truth

In JavaScript, the following code

var answer = 'y';
if (answer) {
  console.log('That is TRUE');
} else {
  console.log('That is FALSE');
}

will indeed write That is TRUE to the console, so it is a valid conditional, even though answer is a String variable and not a Boolean variable.

4 Answers

Yep in javascript Strings are considered truthy except "" which is falsey. This holds even for "0" and "false" both are truthy. Pretty much anything can be evaluated with a conditional in javascript it is not restricted to Booleans.

The falsey values are :

false
0 (zero)
"" (empty string)
null
undefined
NaN (a special Number value meaning Not-a-Number!)

Everything else will be truthy.

Good to know. Thanks Cody. You're the man!

Haha when someone first mentioned the word truthy in relation to JS I couldn't stop laughing. I still can't now. Thanks for the pointer.

Thanks for the extra tip.