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 an Else If Clause

Trying to figure out why else if wont run

I'm getting a parse error. Suggestions?

script.js
var isAdmin = false;
var isStudent = true;

if ( isAdmin = true) {
    alert('Welcome administrator');
}  else if { (isStudent = true) alert('Welcome student') };
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Abbey Tipton
Abbey Tipton
16,846 Points

Hey there, you have a couple of problems that are causing your if/else statement to not work. First, you need to use a === rather than just one = in your if statements, such as:

if ( isAdmin === true) 

There's some good information here: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons if you'd like to read about why.

Secondly, check your syntax after your else if statement. You have the opening curly brace before the if statement, when it should be after.

I hope that helps you!

Steven Parker
Steven Parker
231,236 Points

Abbey is correct but it might be worth pointing out that two or three equal signs are equality comparisons (normal "==", and type-sensitive "==="), but just one is an assignment operator.

Also, it's never necessary to compare anything to true. As shown in the original challenge code, just naming it is enough:

if ( isAdmin ) {