Why is `favorite_Phase === 'Love That!'` required?

For all others who might have questions to the third example. Let’s start from unchanged expression, which looks like this:

let favoritePhrase = ‘Love That!’;

if (favoritePhrase === ‘Love That!’) {
console.log(‘I love that!’);
} else {
console.log(“I don’t love that!”);
}

Now we are asked to change it using ternary operator. To make it properly we need to read very carefully what our condition says:

(favoritePhrase === ‘Love That!’)

That is why to change it properly we need to KEEP condition unchanged. Otherwise, we are answering a different question. Our question is: is this strictly the same type and value (by definition of ===), so using full statement:_____ favoritePhrase === ‘Love That!’ ____ is must have this time if you will. Therefore the proper change will look that way:

favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(“I don’t love that!”);

I hope that is clear and easy now. Please let me know if it helps?

3 Likes

This comparison is a ‘strict equal’ comparison.
Another words it checks value and type.

For example try the following using the ‘strict equality’ and see what you have returned.

let favoritePhrase = ‘100’;

favoritePhrase === 100 ? console.log(‘I love that!’) : console.log(“I don’t love that!”);

Otherwise known as, identity comparison.


Aside

Another words

Sure you meant,

In other words

Yes good pick up, it was an error and meant ‘In other words’

1 Like

I still don’t get one thing.

The ternary operator is supposed to do one thing or the other depending on the result of the evaluation, which may be True or False.
If we keep it as favoritePhrase === 'Love That!, then the result will be True, because the variable and the string are strictly equal. So, it will continue with the first option given by the operator.

I hope I’m right to this point.

Now, what I don’t get is, a “full” string counts as a truthy value, or that’s what I understood. So, the string is truthy, the variable is true so it should still give True as a result and continue with the first option given by the operator, shouldn’t it?

Now, I went back to the code to check it again a few times. I kept it only as favoritePhrase and it still works.
So, my new question is, is this just another one of those cases in which the code is good but it’s not an exact copy of what the system expected as an answer so it takes it as wrong?

Thank you!

but this is above it? ----> let favoritePhrase = ‘Love That!’; shouldn’t that count?

I’m new to this, instead of using words like conditional expression could you explain what that means?

why do we need three ===='s?

favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(“I don’t love that!”);

1 Like

Thank you. This was the only helpful explanation here. Others who respond by pasting more descriptions and code jargon just make it all the more confusing. Glad to see that someone answered this exact question in plain English!

2 Likes

Hello, I find this a great explanation.

Why use the === again, right?

Now, if we fail to use === in an expression, in the ternary operator to test the condition of favoritePhrase, it will evaluate to true, and the second state will never run. This is because without the ===, the computer sees favoritePhrase as a string (not an empty string) and strings that are not empty evaluate to true.

However, if we use ===, then we tell the computer to check for the exact value and type of favoritePhrase and if it is the case that it is of that exact value and type, run the first state or the second state if it is false.

In essence, we are trying to be super careful with the instructions we tell the computer to perform so we are certain we are getting the required output.

I hope this helps @allisonting888gmail ?

Hi I just read all comments from here and I came here for help because I have the same problem what to do
.And Im still a bit confused because when I type
without === I have the same result as if I typed with === .So is it possible to write it without === or that would be just a bad practice?

let isLocked = false;

isLocked ? console.log(‘You will need a key to open the door.’) : console.log(‘You will not need a key to open the door.’);

let isCorrect = true;

isCorrect ? console.log(‘Correct!’) : console.log(‘Incorrect!’);

let favoritePhrase = ‘Love That!’;

favoritePhrase ? console.log(‘I love that!’) : console.log(“I don’t love that!”);

Hi there,

assigning the bolean false to isLocked … the ternary operator will never evaluate the first condition.

The code above will skip the first condition and evaluate the second. This is because:

The condition below, always check for truthy.

Same way, here :point_down:t5:, the first condition using the ternary operator will always evaluate.

Also here, :point_up:t5: a string will always yield true, when applied to ternary operator. (a string, not an empty string)

2 Likes

That is the answer we’ve been looking for.
From my experience on Codecademy, this is the type of simple yet potentially confusing problem that teachingDesigners want us to figure out. Up front it’s not intuitive, but if we really look back on things we’ve been taught through the course, it does make sense.

2 Likes

This was so helpful, thank you so much!

1 Like

You’ve now given a great answer;
‘There can be only one result’.

I’m satisfied with that and I now understand in line with the concept that a string is not a boolean so it can only be one thing. As in, a string has no opposite.

Thank you so much.

1 Like

Am glad there’s a community.
i had basically hit the wall with that!

So, since the code;

if (favoritePhrase) {

console.log(‘I love that!’);

} else {

console.log(“I don’t love that!”);

}

yields an equivalent result, what you mean to say is that when it comes to strings in the case of a ternary operator, one has to be particular? And that’s why it’s advised to use the ‘===’ operator even though a truthy result would still be given if not used anyway?