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 trialMichael Molaro
730 Pointshow do I include TWO false variables in an ELSE statement?
var isAdmin = false; var isStudent = false;
if ( isAdmin ) { alert('Welcome administrator'); } else if (isStudent) { alert('Welcome student'); } else ( isAdmin isStudent) {alert("Who are you?");}
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
} else ( isAdmin isStudent) {alert("Who are you?");}
<!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>
2 Answers
Cody Fisher
12,309 PointsHi Michael,
An else statement will not have a following set of parentheses with a condition in them. Instead, it just has a code block following it. Also, since you have already checked to see if isAdmin and isStudent are true in the previous if and else if statements you do not need to recheck that they are false. Therefore, if you remove the ( isAdmin isStudent )
from your code it will be properly formatted:
else {alert("Who are you?");}
However, if you do want to test for two conditions in one statement you can use the logical and (&&) or logical or (||). In this case you would need to change the else to another else if since else cannot make use of an additional condition (it is kind of like a finally block). Here is how it would be coded for these two conditions:
else if ( !isAdmin && !isStudent ) {alert("Who are you?");}
This states that you would like to print an alert if isAdmin is false AND if isStudent is false.
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsWith "else" you don't use () just {}.
else { alert("Who are you?"); }
Michael Molaro
730 PointsAh! duh. thanks.
Michael Molaro
730 PointsMichael Molaro
730 Pointsthanks!!