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

P J
2,391 Pointsproblem with "else" conditional statement
var answer = prompt('What is the best programming language?'); if (answer === 'JavaScript'); { alert('You are correct') } else { alert('JavaScript is the best language!'); }
4 Answers

Gavin Ralston
28,770 Pointsvar answer = prompt('What is the best programming language?');
if (answer === 'JavaScript'); {
alert('You are correct')
} else {
alert('JavaScript is the best language!');
}
I used markdown to clean up the presentation for you, and to make spotting the issue a lot easier.
Look carefully at your if condition, keeping in mind that if statements are typed like so:
if ( condition ) { code here}
...and semicolons mark the end of a complete statement. Anything after a semi-colon would be considered an entirely separate statement.
Hope that helps!

P J
2,391 PointsThank you so much for the help. I took out few of the semicolons and it worked ... but I'will search for the one in trouble. Weird thing is that debugging sent me to the "else" statement. Gavin , you think I should put the semicolon only after the alert statement, and not after " if (answer === 'JavaScript')" line..... Was that your point ? that seems to be in the middle of the statement, right?

Samuel Webb
25,370 PointsYes, the semicolon should be moved from the if line to the first alert line. The second alert line is fine. The else statement is where Javascript first realized there was a problem. Since else is supposed to come after an if statement, and you closed the if statement with a semicolon, Javascript was confused on why there was an else statement with out the preceding if statement.

Gavin Ralston
28,770 PointsWhen you're looking at the console, it's probably telling you something along the lines of "Unexpected token" which means it wasn't expecting to see an "else" out on its own like that.
For instance, if you do this:
if ( true );
It's evaluating that statement, saying "okay, it's true, so let's do something" and then it hits a semicolon. So the browser literally runs nothing (which is all that was supplied to the if statement) and keeps on going right to the next line to look for more instructions.
Then the next thing it runs into is an "else" statement. Keep in mind the else statement is all on its own, the if condition was already evaluated and the corresponding code (nothing at all) was run. So the browser goes "umm, I wasn't expecting an else without a corresponding if statement there.

P J
2,391 PointsThank you very much for helping me understand the logic behind the code. Really appreciate it !

Gavin Ralston
28,770 PointsNo problem. In the future, be sure to mark a "best answer" or use ratings so people can easily identify what worked for you. Other folks might search and find this same question, and having it already answered means they don't have to wait on a reply.
Good luck!
Samuel Webb
25,370 PointsSamuel Webb
25,370 Points1 misplace semicolon will make Javascript go crazy.