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

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

variables with 'false' Boolean value

Although I've watched the lesson video 3-4 times, I am confused here with what's happening behind the scene.

  1. var isAdmin=false; if(isAdmin=false) // what's actually happening in the if condition test? Am I alerted that welcome admin? but why? Isn't the isAdmin value false? or has it become true for the if condition used?
script.js
var isAdmin = false;
var isStudent = false;

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

else(true) {
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>

4 Answers

Steven Parker
Steven Parker
229,786 Points

For a boolean value, you need to only name it in a conditional expression to test it. So the code: "if (isAdmin)" is checking if isAdmin is true. So given the code shown, it should not show the alert message.

Also, there is an error in the final "else" statement. A plain "else" does not take a conditional expression. It does not need one since it will handle any condition not previously handed by the "if" or "else if" statements.

Hi Rahman, in the challenge the 'isAdmin' and 'isStudent' variables have already been set to false i.e. given a boolean value of false. In a conditional statement, the test in an 'if' statement has two outcomes regardless of the circumstances. The test is either true or false, and if the test is false, the JavaScript interpreter goes to the next else-if clause and checks the test, and so on. If all the tests in the multiple clauses of a conditional statement is false, the code block in the last else clause will run.

Since both variables in the challenge are false, its the code block in the else clause you are to add that will run, and you don't add a test with an else clause in JavaScript because it's the last resort when all possible outcomes is false. So just delete that (true) beside the else clause and you're good to go.

Ying Chen
Ying Chen
2,353 Points

const isAdmin = false; const isStudent = true; let message;

if ( isAdmin ) { message = 'Welcome admin'; } else if { message = "Sorry, you are not an admin"; }

can someone help me please!!!!!

Steven Parker
Steven Parker
229,786 Points

Help you with what? It would be best to start a fresh question using the "Get Help" button on the course page.