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

Saad Shah
Saad Shah
4,006 Points

Conditional statement

Hi Treehousers. This is a very simple exercise but I can't seem to get the syntax right. What am I missing conceptually?

script.js
var a = 10;
var b = 20;
var c = 30;

if ('var a' > 'var b') {
  alert ("a is greater than b");
} else {
  alert ("a is not greater than b");
}
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>

2 Answers

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

Hi there! The problem in your code is that you're comparing the two strings "var a" and "var b".

What you want to compare is the value of your variable "a" and your variable "b". So inside your if statement : a > b is what you need (Is the value of "a" is greater than the value of "b")

Saad Shah
Saad Shah
4,006 Points

Worked like a charm, thanks!

andren
andren
28,558 Points

Your issue seems to be that you are confused about how you are meant to reference a variable. Quotes (single or double) are only used when creating a string. They are not used when referencing a variable. Additionally the var keyword is only used when creating a variable, not when referencing an existing one.

So you have to remove the quotes, and the var keyword like this:

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

if (a > b) {
  alert ("a is greater than b");
} else {
  alert ("a is not greater than b");
}

Doing that will fix your code. As everything else you have written is correct.

Saad Shah
Saad Shah
4,006 Points

Worked, thanks for the explanation!