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 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.