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 a Final Else Clause

Karolina Stachijuk
Karolina Stachijuk
3,024 Points

I have no idea what is wrong with the code, someone have an idea? :)

I have "SyntaxError: missing ; before statement"

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

if ( isAdmin=true) {
    alert('Welcome administrator');
} else if (isStudent=true) {
    alert('Welcome student');
} 
else ( false ) {
  alert('Who are you?');
} 
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

Samuel Webb
Samuel Webb
25,370 Points

The = operator is called the assignment operator. So basically it assigns whatever is on the right side, to be contained inside of whatever is on the left. You did this when you set isAdmin and isStudent.

Your problem starts when you're trying to check if they're true by using that same assignment operator. What you should be using is the strict equality === operator. isAdmin === true Same goes for the isStudent check.

Second problem is that you don't need to check for anything after an else statement. else basically means, anything that isn't covered by the previous checks, check them here. So you can remove that (false) all together.

Finally, you forgot to end you else statement with a closing curly brace.

Here's how your code should look:

var isAdmin = false;
var isStudent = false;

if ( isAdmin === true) {
    alert('Welcome administrator');
} else if (isStudent === true) {
    alert('Welcome student');
} 
else {
  alert('Who are you?');
}
Samuel Webb
Samuel Webb
25,370 Points

Also, when checking for true or false values, it is faster and less code to do it the way it starts off in the code example.

(isAdimn)
// instead of
( isAdmin === true)