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 trialFairen Baker
1,892 PointsI can't figure out what's wrong with my "else" statement
I can't figure out what's wrong with my "else" statement
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator')
} else if (isStudent) {
alert('Welcome student')
}
else( false){
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>
3 Answers
Trevor Skelton
1,101 PointsThe 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
15,622 PointsHi 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
}
Warren Leyes
Front End Web Development Techdegree Graduate 31,808 PointsAlso if(notWorking){document.write('check your semicolons');} else { document.write ('also check the arguments for: if elseif and else'); }