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 Using Comparison Operators

The else clause is not being recognized with the alert command

I followed the instructions and now the challenge is telling me that the else clause alert is not there, when I clearly put it there.

I even put the curly brackets around the else clause to see if that was the problem but it made things worse so I left them out. Is there something I'm missing to solve this problem. If there is, could you explain what I did wrong so I can understand?

Thank you kindly!

var a = 10;
var b = 20;
var c = 30;

if ('a > b')
  alert ('a is greater than b'); 

else ('a < b')
 alert ('a is not greater than b');
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="script.js"></script>

2 Answers

Steven Parker
Steven Parker
229,920 Points

The message may be a bit misleading, the actual issue is that an "else" does not take a comparison expression. It doesn't need one since it handles anything not covered by the "if".

Yeah that's what I thought but if I remove the comparison expression, I get this error message:

"I don't see the message 'a is NOT greater than b'. Are you sure you wrote the conditional statement correctly?"

Steven Parker
Steven Parker
229,920 Points

I missed it the first time, but you don't want to put quotes around your conditional expression. That would make it a string literal, which by the fact that it's not empty is always "truthy".

Thank you Steven, that helped when I removed the quotes. I'm still having a hard time determining when to or not to put quotes in something. Thanks for your help!

Steven Parker
Steven Parker
229,920 Points

This hint might help: Generally, you'll never put quotes around a variable name, or an expression that uses an operator for calculating or comparing anything. Putting quotes around something makes a string literal, which doesn't represent anything except for the actual letters and symbols in the quotes. For example:

console.log("23 + 17");  // the console will show:  23 + 17
console.log(23 + 17);    // the console will show:  40