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 trialSean Ford
9,581 PointsWeren't we told not to use semicolons in conditional statements?
This question has a semicolon after the 'if' statement, yet we were taught not to use them at those times.
Sean Ford
9,581 PointsHere is the question from the "Review Conditional Statements and Comparison Operators" quiz with the given code:
When a browser runs the code listed below, what will the user see?
var spaceShips = 0;
if (spaceShips === 0 ) {
alert('You lose!');
} else {
alert('You are still alive!');
}
Sean Ford
9,581 PointsGreat, thanks for clearing that up!
2 Answers
Marcus Parsons
15,719 PointsThose semicolons are used on the commands within the if-statement, not on the if-statement itself. You want to use semicolons on any statement within the if-statement. Semicolons are not absolutely necessary in JavaScript but they are a best practice so that you can avoid potential debugging nightmares.
Jason Anello
Courses Plus Student 94,610 PointsDo you remember which video this was in so I could review?
I can only guess that Dave was saying you shouldn't put a semi-colon at the end of an if
statement like this:
if (spaceShips === 0 ) { ;
In this quiz question there are only semicolons at the end of the variable declaration statement and the 2 alert statements which is perfectly fine.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Sean,
Can you post the question? The quizzes are sometimes randomized and I've gone through it a few times and can't find a question with a semi-colon at the end of an
if
statement.