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