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

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.