FAQ: Conditional Statements - Comparison Operators

This community-built FAQ covers the “Comparison Operators” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Comparison Operators

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Whats the different between two (==) and three (===) equal signs?

I couldn’t find out what I was doing wrong, then I saw that the last statement had an exclamation point and I had put a period. Realistically, if I had made that mistake, I don’t think it would’ve prevented me from going forward. I think that’s a pain in the ■■■ to have to deal with. If it’s in the ’ ’ then you can put whatever you want, isn’t that correct??

2 Likes

Unfortunately all the Codecademy lessons are like this. You just gotta learn to live with it.

9 posts were split to a new topic: Did I find a bug?

A lot of mistakes are made in code where developers simply miss details. This causes code to be commented and sent back for review. So while I agree with you that it’s a pain in the ■■■, it teaches you to be attentive to the little details that make a difference in the end.

2 Likes

Realized I had the condition outside the parenthesis. Worked this time:

let hungerLevel = 7;

if (hungerLevel > 7) {
console.log(‘Time to eat!’);
} else {
console.log(‘We can eat later!’);
};

8 Likes

How is:

let hungerLevel = 4;
if(hungerLevel > 7) console.log("Time to eat!");
else console.log("We can eat later!");

still triggers Did you add an if statement that checks the condition hungerLevel > 7? even when the output is technically correct?

the exercise validation seems to have problems with this shorter syntax.

Why does the example and lesson text have multiple instances of === meaning “equal to” but the terminal will only accept a single = as a valid answer.

Ridiculous!

because assignment (=) and equal to (===, this is a comparison operator) are two very different things.

1 Like

Hi! I’m having an issue where even though all the syntax is apparently correct and the code runs, it ignores? the > operator and always prints the else condition. Using the >= operator does print the if.

Why is this happening?

Thanks!

Can you provide a code sample of this? Such a broad description means i have to guess what is going on

Hi, I got quite stuck on this one but managed to figure it out using this code:

let hungerLevel = 7;

if(hungerLevel > 7) {
console.log(‘Time to eat!’);
} else {
console.log(‘We can eat later!’)}

hope that helps

2 Likes

It works but i think you have less than in place of greater than

What is going on here? This seems a likely mistake.

let test = 4;
//test =< 5; //SyntaxError
//test => 5; //no error
if (test => 6){
  console.log(test);
}
// prints 4
if (test = 7){
  console.log(test);
}
//prints 7

Hello, @miksupy2553930348, and welcome to the forums!

I’d suggest reading the docs on comparison operators. I’ll add a few comments to your code to explain a little about what is happening:

let test = 4; //assigns 4 to the variable test
//test =< 5; //SyntaxError **there is no =< operator, hence the syntax error
//test => 5; //no error **this line is meaningless. Technically it's a function, but it doesn't do anything. 
//The => is referred to a a fat arrow, and has use in arrow functions.
if (test => 6){ //here you have if evaluating whether the value inside the parenthesis is truthy. Since the value is a function technically, it has a bool value of true, and the code inside the if block executes
  console.log(test); //prints 4 as you've noted because test still points to that value
}
// prints 4
if (test = 7){ //here you are assigning the value 7 to the variable test, and then if is testing whether the result is true. It is (any numerical value other than 0 is truthy), so the code inside the if block executes.
  console.log(test); //prints 7 as you've noted since you just assigned 7 to test
}
//prints 7

If you’re curious about which values are truthy or falsy, you can use the Boolean() function to check for yourself. Here are a few examples:

console.log(Boolean(test => 6))//true
console.log(Boolean(0))//false
console.log(Boolean(-1))//true
console.log(Boolean(""))//false
console.log(Boolean("a"))//true
console.log(Boolean([]))//true (empty array)
console.log(Boolean({}))//true (empty object)

Output:

true
false
true
false
true
true
true

1 Like

Conditional Statements - Comparison Operators:

Hello,
I’m really not sure there’s hope for me, just a newbie. I get the basic concept of the IF, Else statement but guess my question is in the area of how to actually write the code? Not even sure how to explain my question…

Here, I’ve changed the comparison operator to < 7 instead of > 7 and the output is ‘We can eat later.’ But it’s confusing me because I feel like I should change the code to this:

let hungerLevel = 7;

hungerLevel = 10;

if(hungerLevel < 7) {
   console.log('We can eat later!');
}
else {
  console.log('Time to eat');
 }

Instead of:

let hungerLevel = 7;

hungerLevel = 10;

if(hungerLevel < 7) {
   console.log('Time to eat!');
}
else {
  console.log('We can eat later!');
 }

Hope this makes sense. If not, maybe we can work through it eventually…Thank you for your time…

So think I’ve figured it out…

You set up a condition for a certain situation and then depending on the answer that comes in from the user, depends on what outputs…?

So in this case, there would be no need to change the > to <. It would stay as it is.

I was just experimenting with changing the comparison operators around as they said to do after the exercise was done, but in doing so think I confused myself!

This might be a silly question, but what exactly tells the computer that one is greater than zero, or 8 > 7, such that it can evaluate that statement/condition/argument to be true?