Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Introduction to Programming Control Structures If/Else

R R
R R
1,570 Points

Why does the If block run even if I plug in a false-e value for my variable?

My "if block" runs if I plug in a name into the prompt just like it should. But, I assumed that the else block would run if i plugged in 0 or null into the prompt. But, the if block runs regardless. Is this because the code sees anything plugged into the prompt as a string/text rather than as code?

onsole.log("Before");

var name = prompt("What is your name?")
if (name) {
    console.log("if block")


} else {

    console.log("else block")

3 Answers

prompt() will always return a string. So if you put 0 or null in you're going to get "0" or "null" back.

R R
R R
1,570 Points

Thank you, Geoff. This noob appreciates the help. :-)

William Titterton
William Titterton
10,960 Points

you can also return an integer from a prompt value using the 'parseInt(variable_name)' function

Matt Varner
Matt Varner
5,373 Points

Hmm...I have to try that.