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

Justin Townsend
Justin Townsend
4,720 Points

Else if statment

I am not really sure what to do here!

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

if ( isAdmin ) {
    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>

2 Answers

jamesjones21
jamesjones21
9,260 Points

Hi Justin, you are almost there just need to do amend the syntax for the else condition, as it checks against the variable isAdmin, and as a single condition it will check if true / false, so if the isAdmin is false it will go to the else statement and alert 'Welcome student'

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator');
} else {
alert('Welcome student');
}

but using an else if condition would look like this:

as with else if they also need open and closing parenthesis to include the condition you want to test against.

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if(isStudent){
   alert('Welcome student');
}

hope this helps :)

Justin Townsend
Justin Townsend
4,720 Points

Thanks James this really helped!