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 trialR R
1,570 PointsWhy 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
Geoff Parsons
11,679 Pointsprompt()
will always return a string. So if you put 0 or null in you're going to get "0" or "null" back.
R R
1,570 PointsThank you, Geoff. This noob appreciates the help. :-)
William Titterton
10,960 Pointsyou can also return an integer from a prompt value using the 'parseInt(variable_name)' function
Matt Varner
5,373 PointsHmm...I have to try that.