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";
console.log(a);
let f = x => x ? 'something is there' : "something isn't";
console.log(f(moreTest));
console.log(f(test))

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} !):

console.log(Hello!);

let userName =‘J’;

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

console.log(Hello!);

You can get more clarity if you change the value assigned to your variable favoritePhrase to something different from ‘Love That!’

ie:
let favoritePhrase = ‘Loves That!’

In this case, if you write

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

you’ll get a “I don’t love that!” print.

the operator:

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

simply checks if the expression is true.
Anything but a 0, or null, or NaN, etc (that is, any non-falsy value) will print “I love that”.

If you want the print to be “I love that!” ONLY IF the value is SPECIFICALLY the string ‘Love That!’
(and not, ie, ‘Loves That!’, or ‘L0ve That’ etc), then the solution is to specify “if favoritePhrase is specifically the exact string ‘Love That!’”, or, in code:

favoritePhrase === ‘Love That!’

There seems to be an issue with the quotes in your code.

If you replace the smart quotes ( and ) with regular quotes (' and "), then your code will work as expected:

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

This code uses the ternary operator to check if the value of favoritePhrase is equal to the string 'Love That!'. If it is, it logs 'I love that!' to the console, otherwise it logs "I don't love that!".

This is the simplest answer – the original condition was checking the value and type (===, or as others pointed out an identity comparison); if we change the condition in the abbreviated code with the ternary operator (like simply checking if the variable has value/evals as true, i.e. favoritePhrase ? 'valueIfTrue', you’re changing the conditions for how the code can evaluate. It’s not only checking if it was true/false (or had value/no value), it’s checking specifically that the variable was a string and that the string value was ‘Love That!’.

Both will run, and they’ll even produce similar results in the lesson, but without the identity comparision (===), it’s not logically the same evaluation as the original conditions, and so is not correct.

The expression favorite_Phase === 'Love That!' is required in JavaScript for a specific purpose: to compare the value of the variable favorite_Phase with the string 'Love That!' to determine if they are equal.

Here’s why it might be necessary:

  1. Conditional Logic:
  • The === operator is used for strict equality comparison in JavaScript. It checks both the value and the data type of the variables being compared.
  • When you write favorite_Phase === 'Love That!', you are essentially asking JavaScript to check if the value stored in the variable favorite_Phase is exactly equal to the string 'Love That!', including both the value and the data type.
  • This is often used in conditional statements like if or switch to make decisions based on whether a certain condition is met.
  • For example, you might want to perform a specific action only if favorite_Phase is equal to 'Love That!':

javascript

  // Do something special
}


  1. Comparison and Validation:
  • It’s common in programming to compare variables or user inputs with specific values to validate or control the flow of a program.
  • By using ===, you ensure that you’re making an exact and strict comparison without any type coercion. This can prevent unexpected behavior in your code.
  • For instance, if favorite_Phase is expected to be exactly 'Love That!' and not some other value like 5, the strict equality check will catch that difference.

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

2 Likes