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

what is the reason that if you type true it prints out "the condition is true" and with false it prints out "the conditi

what is the reason that if you type true it prints out "the condition is true" and with false it prints out "the condition is false". I know he said it, but WHY does it prints out true or false. if i put in a number , then i also get a true response. and if i put in random letters i get a false. WHY is that so? could anyone explain this to me, whit a view more words? love to hear from you!

if (5) { console.log('The condition is true.'); } else { console.log('The condition is false.'); } ---> true

if (qwerty) { console.log('The condition is true.'); } else { console.log('The condition is false.'); } ---> false

if (false) { console.log('The condition is true.'); } else { console.log('The condition is false.'); } --> false

2 Answers

Steven Parker
Steven Parker
231,141 Points

The "if" expression is converted into a Boolean (true or false) value if it is not one already. Several rules apply to how things get converted. Any number other than zero is converted to "true", but 0 is considered "false".

When you put "random letters" you are creating a variable name. If the variable has not been previously created, this should cause an error in most JavaScript systems. But if yours allows it, another rule says that anything that is undefined converts to "false".

thank you so much. i understaind it now !