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 JavaScript Basics (Retired) Making Decisions with Conditional Statements Add a Final Else Clause

Daniel Deering
Daniel Deering
2,994 Points

Parse Error

Not really sure what is wrong with my code.

var isAdmin = false;
var isStudent = false;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else (false) {
  alert('Who are you?');
}

I have also tried to add (isAdmin === false) + (isStudent === false), all I get is a parsing error.

Thanks.

2 Answers

huckleberry
huckleberry
14,636 Points

Heya Dan!

The problem you're having there is that you've added a conditional to the else portion of your code. Else statements don't get conditions.

Things That Get Conditions in an If statement

  • if gets a condition
  • else if gets a condition
  • else does not get a condition.
if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else (false) { //<---- this right here... there should be no (false) condition
  alert('Who are you?');
}

The else part of the whole thing is there to execute when the if or else if does not meet the condition. It's the "plan B" so to speak. It's executing based on whether or not the condition after the if (or else ifs that are present) is met. Therefore, a condition somewhere before it has already been evaluated and thus is not needed after an else :).

Hope that helps!

Cheers,

Huck - :sunglasses:

Daniel Deering
Daniel Deering
2,994 Points

Awesome, thank you. Can't believe I didn't catch that.