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

What am I missing here?

Not sure what else to add here. Thanks!

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

if ( isAdmin ) {
    alert('Welcome administrator');
} else if {
    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>

All the answers below are true. I would add that when using the else if clause it doesn't end the conditional statement, it merely adds an additional if clause to the condition and as such needs to have its own conditional clause in this case ( isStudent ) is that condition. the conditional clause is omitted only on the else clause.

2 Answers

Else...if conditionals need a few things to work:

  1. If clause | expression (true/false) | statement (alert('hello');)
  2. Else if clause | expression (true/false) | statement (alert('hello');)
  3. Else clause | statement (console.log('error');)
//Variable Values
  var isAdmin = false;
  var isStudent = true;

// This Else...if condition will alert isStudent
  if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else {
    console.log('check var values');
}

take a look at the MDN for further details on conditional statements

jason chan
jason chan
31,009 Points
var isAdmin = false;
var isStudent = true;

// if admin is false do nothing
if ( isAdmin ) {
    alert('Welcome administrator')
// else if isStudent is true do something
} else if ( isStudent) {
// alert the string below
alert('Welcome student')
}

That's the answer. Oops. LOls

else if (true) {do something}

in this case alert.