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

Fairen Baker
Fairen Baker
1,892 Points

I can't figure out what's wrong with my "else" statement

I can't figure out what's wrong with my "else" statement

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

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

3 Answers

Trevor Skelton
Trevor Skelton
1,101 Points

The else statement doesn't take parameters. It is there when every other if is false, e.g.

if(light == on) {do something} 
else {do something}

If you need parameters then it is simply an else if block, e.g.

if(light == red) {stop} 
else if (light == green) {go} 
else if (light == yellow) {slow down} 
else {light is broke}
Hugo Paz
Hugo Paz
15,622 Points

Hi Fairen,

With else statements, you dont have a condition, so you just write:

else {
//code to run
}

You always check the condition with an if and if there is something you want to happen if the condition fails, you use an else.

if(condition){
//condition is true - do something
} else {
//condition is false - do something else
}