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

This is the perfect explanation to that piece of logic!! I got it wrong so many times until I saw this example! :)))))

1 Like

Yo en tiendo así:
cuando escribimos console.log(‘I love that.’) eso es lo que le mostramos o imprimimos a la consola del navegador en mi caso google Chrome.
Para que eso pudiera imprimir se tuvo que la variable let favoritePhrase fuera igual a love that y se cumpliera que fuera verdadero.
en pocas palabras nosotros podemos declarar variables y imprimir a la consola cualquier cosa que se nos ocurra siempre y cuando esas variables sean igual definida al proceso del operador ternario ya que la computadora no sabe o no tiene un pensamiento lógico en cuanto a lo que se imprime o sea el console.log()

I thought the same thing as cgg, that the past two examples were favouratePhrase ? console-log(‘I love that’) : console.log (“i don’t love that”); I assumed that the first case was true and the second was false. I had the idea and was going well understanding these statements, until i saw the answer. then confusion set in. Just a little confused. It it is an exception to the rule then ill just have to understand.
Thanks Cgg, mtf, chipblaster, and others.

so after reading the other reply on here i figured out the answer but i feel like a more concise explanation for those who come after me wanting to know the same thing wont go amiss.

the reason the correct solution to the 3rd problem is
favoritePhrase === “Love That!” ? console.log(“I love that!”) : console.log(“I don’t love that!”)

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

is that the variable favoritePhrase does not have an inherently true or false statement to it unlike the other two variables in this example which are
let isLocked = false;
let isCorrect = true;

isLocked is false and isCorrect is true we don’t need to ask questions for these since they are the answer.

where as favoritePhrase is a string and therefore needs a question for this exercise.
now the reason why you don’t get an error and even get the right output is because lacking a defining question to ask favoritePhrase it defaults to a truthy falsey check which is just asking if favoritePhrase exists at all as any kind of information

1 Like

From reading this whole thread I think this is really the most helpful example. This explains precisely why the identity operator is needed. Thanks for the great answer :+1:

2 posts were split to a new topic: Why the exercise returns ‘I love that!’

I think both cgg and mtf are correct!

let favoritePhrase = ’ Love That!’;
// either 1.
console.log(favoritePhrase === ‘Love That!’ ? ‘I love that!’ : “I don’t love that!”);
// or 2.
console.log(favoritePhrase ? ‘I love that!’ : “I don’t love that!”);

Correct me if I’m wrong! The string value ‘Love that’ is committed to the memory at variable favoritePhrase. The ternary 1 would provide more clarity to the user/programmer. Whereas ternary 2 would also provide the correct answer as the favoritePhrase is truthy for it contains a string value ‘Love that!’ but nothing else (you can test with a different value and see the response); it, however, isn’t clear to a human reader as to what value it was assigned to (unless one looks at the previous lines of codes) until the program finishes its execution, especially in a very long program.

mtf explains that the program doesn’t know its dynamic state, true… until the result is assigned (committed) to a memory pointer. Unless its value is changed intentionally, otherwise it will be the same throughout the program when the same variable is called for. Again, only the computer “knows” what it holds in the memory, it might be a better practice for us to provide clarity!

I’ll use an example of a website that requires a user to prove who he says he is before logging in. The website asks for the user’s social security number against its database of thousands of members:

let userInput = (user input here);
const userSocialSecurityNo = ‘123-45-6789’;

console.log( (user input) === userSocialSecurityNo ? ‘Welcome aboard!’ : ‘Please try again!’ );

Had it been just an evaluation of and by itself (user input) only, anyone could log into the website with a fake number (except zero or empty, which is falsy)!
In the case of the example illustrated by the lesson, there’s only one value committed to the memory (i.e., “Love That!”) so though

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

provides the answer, but it’s undesirable for safe practicing, IMO.

1 Like

I appreciate the Hitchhikers Guide to the Galaxy reference :smiling_face_with_tear:

1 Like

Hi all, the way I see it is as follows:

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

The above works when executed in VScode and should be the preferred solution (in my view). As favoritePhrase has already been assigned a valid string automatically makes it a truthy value, hence “I love that!” will be logged to the console. Making use of an assignment operator in the solution (in this case) only makes it more verbose and isn’t necessary, defeating the purpose/real benefit of a ternary.

No, the reason we need the assignment operator (===) is because we are not just asking if favoritePhrase is true or false in this instance. We are asking if favoritePhrase “is equal to” what it was called out to be, as in is identical to.

As long as favoritePhrase is not an empty string, the following code will always read favoritePhrase as true and show ‘I love that!’

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

However, if we want the computer to actually read the string and make sure it “matches” the string assigned to favoritePhrase, we would need to write the code as such:

favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(‘I don’t love that!’);
or even simpler,
console.log(favoritePhrase === ‘Love That!’ ? ‘I love that!’ : I don’t love that!’);

This way, if favoritePhrase is input as any string other than ‘Love That!’, the second statement would be returned.

Hope this helps.


Agree. This was the only answer that finally helped me understand why the first 2 did no require what the 3rd did. Thank you so much!

The assignment operator is needed because you are testing whether or not the value in favoritePhrase equals ‘Love That!’.

Imagine if we set favoritePhrase as a value such as, "I really don’t love that’. The conditional would still be evaluated as Truthy (because strings are Truthy) and will output “Love That!” – clearly not what we want.

So, we use the assignment operator before the ? (ternary operator) to check whether or not favoritePhrase = “Love That!”. If it they equal to each other, then the conditional evaluates as true and outputs “Love That!”

Don’t think the lesson was very clear here. Codecademy should probably explain that when comparing two strings, an assignment operator needs to be used, even if it is before a ? (ternary operator).

1 Like

Thank you!
Many thanks for not adding a bunch of extra yadda yadda yadda to make the answer confusing!!!
Now I understand!

Okay, so if I understood this correctly, on this part :

let favoritePhrase = ‘Love That!’;

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

You still need a condition to execute this string of code, else it would always return false. Or we can ditch the favoritePhrase === ‘I Love That!’ part in favor of just favoritePhrase ?. I am so confused.

Hi. why we should wright
let favoritePhrase = ‘Love That!’;
favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(“I don’t love that!”);
why we just can’t wright
let favoritePhrase = ‘Love That!’;
favoritePhrase ? console.log(‘I love that!’) : console.log(“I don’t love that!”);
like here:
let isCorrect = true;
isCorrect ? console.log(‘Correct!’) : console.log(‘Incorrect!’);
I don’t understand the logic behind it.

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

let test = ‘a thing’;
test ? console.log(‘something is there’) : console.log(‘something isnt’); -prints- something is there

let moreTest = ’ ';
moreTest ? console.log(‘something is there’) : console.log(‘something isnt’) -prints- something isnt

Just as an aside with respect to ternary expression statements:

While they may work as expected with included statements, they are not intended for that usage. The code you have above belongs in an if..else statement.

if (moreTest) {
    console.log('something is there')
} else {
    console.log("something isn't")

Consider the basic syntax:

expression ? expression : expression

That is where the name comes from: ternary, meaning three expressions.

The first one is a conditional but only in terms of evaluation, truthy or falsy. The second of course corresponds to the truthy outcome and the third the default outcome.

console.log() is a statement that can take any argument, even a statement, but most generally, an expression. Expressions always evaluate to a value. So we can simply plug our expression into the method argument and it will evaluate the expression before it logs anything.

console.log(moreTest ? 'something is there' : "something isn't")

Above we use the ternary in a manner that is consistent with its design.

Also note that given it is an expression, we can assign it to a variable, or return it from from a function.

let a = moreTest ? 'something is there' : "something isn't";
let f = x => x ? 'something is there' : "something isn't";

thank you. based on the questions i was reading in regards to the favoritePhrase = ‘Love that’ question i was curious to see if the absence of favoritePhrase === ‘Love that’ would still give some sort of response. as the first two questions had true-false values. i wanted to see if in the absence of a ‘string’ the log would be false n vice versa. so is what your saying that ternary is only used for true false values? and using a string, or lack there of does not belong? and i apologize, im sure you can tell im very new to this.

1 Like

A ternary does not have built in default values (true, false). It only evaluates the truthiness of the signature expression. That expression can be anything that can be evaluated, string, number, expression, etc. It we want the return values to be boolean, then it would be simpler just to use a function:

const truthy = x => ! ! x;

let isTruthy = truthy('a' * 42)    //  'a' * 42 is NaN

console.log(isTruthy)  // false

That’s one way, at least. The function is simple, it returns a double NOT of the passed in argument. That will always be a boolean. So a truthy expression will return true and anything else will be false.

It’s okay to be a little confused with this. Any learner will be. Take it in, and slowly work your way ahead. There will be plenty of time to improve your understanding as you go. And we’re always here to help, so never feel alone and never avoid asking questions. They are what keeps your steam up, rather than just searching the web (or in addition to).

1 Like