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