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

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

FTR, the above is actually switched around. The model uses a predicate function which always returns a boolean. The name I previously gave the variable should have been given to the function.

const isTruthy = x => ! ! x;         //  predicate function

console.log(isTruthy('a' * 42))      // false
console.log(isTruthy(!(0 && 42 % 7)))   // true
1 Like

Hi guys. I don´t know how to write a new post here without commenting an old one. That´s the reason why I am putting my question here as a comment.

So my question is. " why is the print “Hello” when I write these 2 different codes?

let userName =!‘J’;

userName === ‘Jane’? console.log(Hello, ${ userName} !):


let userName =‘J’;

userName === !‘Jane’? console.log(Hello, ${ userName} !):