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

If/Else

I've been doing just what was in the video and it was said if I left in prompt message empty the Else Block will appear. But I keep getting If block? O_O

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

console.log("Before");

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

console.log("After");

3 Answers

James Barnett
James Barnett
39,199 Points

I think I see where your confusion lies with difference between name and "name", the former refers to the contents of the variable name whereas the later refers to the literal string name.

Hi, remove the apostrophes in name, name is a variable and as your using it right now is a string wich always be true.

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

console.log("Before");

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

console.log("After");

oh and add a semicolon at the of the console.log in the else block.

Thank you so much! ;)

No problem. Glad to help.