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 Review Conditional Statements and Comparison Operators

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

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

Here 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!');
}

Great, thanks for clearing that up!

2 Answers

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

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