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!

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


Joel K
Joel K
3,845 Points

Question on Intro to Programming If/Else - Truthy or Falsy Results

Hi There. On the "Intro to Programming Course" i'm struggling with a concept. What should probably be a simple question i'd imagine.

This is my code.


var name = prompt("What is your name");

if (name) { console.log("If block"); } else { console.log("Else block"); }


When i execute the code and enter a name it returns the if block result which is expected as the (name) value is truthy. However when i enter something like false or 0 as my name it still comes back with "if block". The only time it comes back with the else is if i leave it blank (which is like null). Isn't it supposed to come back with the else block if I type in false or 0 or something falsy?

Thanks a bunch.

2 Answers

The prompt() method always returns a string. For example, when you type


into the prompt box, the string "0" is returned rather than the number 0. So once the input from the prompt box is received,

name = " 0 ";

rather than

name = 0 ;

And the string "0" is truthy.

Joel K
Joel K
3,845 Points

Ahhh got it. Prompt always returns a string. Thanks!