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?

7 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!

1 Like

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!

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 ?

5 Likes

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?

Because when you write out favoritePhrase ? you’re asking if favoritePhrase was assigned a variable not if the variable contains ‘Love That!’
when you add === ‘Love That!’ you are now not asking if favoritePhrase has a variable assigned to it. You are asking if the variable is equal to ‘Love That!’

5 Likes

no because if you omit === you are now asking if isLocked contains a string which in this case will still be true. if you delete =false and you are just left with let isLocked then when you try your code omitting the === it will come out false because there’s no string assigned to the variable let isLocked

from my understanding, when a variable contains a something in quotes, you have to let the computer know what is inside those quote using === ‘quote’ you don’t need to use === when the variable is a boolean, number, or equation.

1 Like

When used in Boolean Condition evaluates to true.
favorite_Phrase has a truthy value ‘Love That!’;
favorite_Phrase === 'Love That! ? console.log(‘Love That!’) : console.log(‘I don’t Love That!’);

The first expression in the condition evaluates to true and is then executed. The second expression in the condition is not evaluated.

this one threw me for a loop the first time i looked at it too.

but if the code were written like this instead:

let favoritePhrase = ‘Hate That!’;

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

…then it would still print ‘I love that!’ , even though favoritePhrase is assigned the string ‘Hate That!’, which is not the desired effect and makes no sense semantically, even if it works syntactically. the reason this doesn’t work out as planned, is because the ternary operation above does not check whether favoritePhrase is equal to the string ‘Hate That!’. Rather, it only checks whether the variable favoritePhrase exists, which it does, and so it evaluates to true, no matter what value you’ve assigned to favoritePhrase (even if it’s the opposite meaning of what you want printed out).

if you want to print out ‘I love that!’ in accordance with the value stored in favoritePhrase (i.e. ‘Love That!’), then you have to compare your variable assignment:

let favoritePhrase = ‘Love That!’;

with:

favoritePhrase ===‘Love That!’ ?

if you only use favoritePhrase ? in your comparison operation, then the computer only checks that the variable favoritePhrase exists, and has no idea what value it has been assigned, and therefore cannot compare meanings.

essentially, you can’t compare a boolean expression (favoritePhrase ?) to a string expression (favoritePhrase = ‘Love That!’) and have it make any sense, because favoritePhrase can be assigned anything.

using only favoritePhrase ? would be like asking someone “do you have a favorite phrase?” and then without asking them what that phrase is you assume their favorite phrase is “Love That!”.

whereas, if you use favoritePhrase === ‘Love That!’ ?, it’s like asking someone “is your favorite phrase ‘Love That!’ ?” and then waiting to see if their answer matches your suspicions.

Hi,
Here’s a suggestion. I used this myself to try to rationalize the code. You may have written some long complicated code and have a variable that changes value a lot as it circulates through several conditions.
But you set it up initially as:
let favouritePhrase = ‘Love That!’;

in other words "Make ‘favouritePhrase’ equal to ‘Love That!’;

and you want some output for when it returns to that value:

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

in other words, “Is ‘favouritePhrase’ the same as ‘Love That’ ? If so, print ‘I love that!’, otherwise print ‘I don’t love that!’”

In our language ‘equal to’ (=), and ‘the same as’ (===) are synonymous but to a computer they have different functions. We create the equality with ‘=’, and the computer checks for it with ‘===’.

I’m not sure how correct this is but I hope it helps anyone trying to get their head round it like myself.