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

So in the exercise it said this was the correct answer

let favoritePhrase = ‘Love That!’;

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

how come it still goes through when the favoritePhrase equals something different then what the console has in it. The only difference is the ‘I’. How come it still goes through when the favoritePhrase doesn’t equal whats in the console?

3 Likes

The string literal in the condition does not have any relation to the string literals in the logged values. Those are outcomes relating to do or don't.

1 Like

The solution is presented as below in this exercise:

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

Because we are asked to refactor the if...else block to use a ternary operator, shouldn’t there be only a ? instead of === ‘Love That!’ just like in the two previous steps?

1 Like

The ternary operator needs a conditional expression to precede it, hence, ?

'is this true' ? 'yes' : 'no'

console.log(a === 42 ? "The meaning of life" : "Nope!");

Notice that the entire expression can be written as an argument to a function or method?

The lesson solution follows this pattern…

conditional ? action if truthy : action if falsy

The ‘action’ is itself a statement. In my example the ternary is what gets passed to the action.

1 Like

Thanks, mtf. Maybe I should have shared more of the code here to make my question clearer:

let favoritePhrase = ‘Love That!’;

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

I guess my question is… can we not simply write favoritePhrase ? instead of favoritePhrase === ‘Love That!’ ? as the value has already been assigned to the variable earlier?

2 Likes

We need the complete expression since we are testing whether the variable was set, or not, to the correct value. Think of a dynamic state where the program doesn’t know this.

a = Math.floor(Math.random() * 42) + 1
console.log(a === 42 ? "The meaning of life" : "Nope!");
// Nope!

The test doesn’t know the value of a but insists that it is 42 in order to follow the truthy branch.

conditional ? truthy branch : falsy branch

A conditional can also be a state.

r = Math.floor(Math.random() * 2);    // 0 or 1
console.log(r ? 'set' : 'not set');   // not set on 0
1 Like

Thanks again, mtf. I believe we didn’t need to test it (whether the variable was set, or not, to the correct value) with the following code (that was written earlier in the same exercise) as in those cases the variables were already assigned to true or false. Did I get right?

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!’);

Keep in mind, it is you who knows the value that is set, not the computer. We write it in the code so we must know it. The computer does not. Remember, computers are dumb. Since it is the computer running our program it falls to us to be sure there is correct information in the pike. That takes testing (conditionals).

Variables that are set have state; those that are not are undefined. There are lots of ways to consider state, so don’t carve it in stone.

let z
console.log(z)    // undefined
console.log(z ? 'state is truthy' : 'state is falsy')   // state is falsy
z = 0
console.log(z ? 'state is truthy' : 'state is falsy')   // state is falsy
2 Likes

Hello! I’ve read the above threads and can’t find the answer I’m looking for.
My question: In the below solution, I don’t understand why we don’t write isfavoritePhrase
Why does the solution omit the “is?”

The first two lines have the is. Thank you!!

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 === ‘Love That!’ ? console.log(‘I love that!’) : console.log(“I don’t love that!”);

3 Likes

The is is part of the variable name in each case. It has no significance other than readability. The variables, isLocked and isCorrect are booleans, so it helps the reader to understand by adding is to the beginning of the name. It’s kinda like saying, “Is the door locked?” The favoritePhrase variable is just pointing to a string. It’s kinda like saying, “My favorite phrase is, Love That!”
This code would produce the exact same results, but may be harder to follow:

let hotDog = false;

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

let potato = true;

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

let p = ‘Love That!’;

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

Hope this helps!

1 Like

I guess so. so, use is__ ? with a boolean and just ? with a string.
or maybe the is isn’t necessary
thank you for helping!!

1 Like

In the lessons, you have to use the variable names they ask you to use in order to pass each step. In your own programming, you can name most any variable almost anything you want (there are exceptions like keywords ie. for, let, const, if, etc.). Using names like isHot gives the reader a clue that it’s probably a boolean, but it’s not necessary, and certainly not required. You will eventually come up with your own naming schemes. The first person to not confuse is yourself, the programmer. If you can make it easier for other programmers to read, that’s great, but whether or not your code works as expected won’t be impacted by how you name your variables.

1 Like

Just re-read your comment, and might be able to clarify further.
The ? is the ternary operator. It’s like shorthand for an if statement.
This:

let isHot = true;
isHot ? console.log("Ouch! That burns!") : console.log("Whew. Glad it wasn't hot!");

is the same as this:

let isHot = true;
if (isHot) {
  console.log("Ouch! That burns!");
} else {
  console.log("Whew. Glad it wasn't hot!");
}

In the example using the ternary operator ? the : represents else. If the condition on the left of the ? is true, the code before the : is executed. If it’s false, the code following the : is executed. In either case, if we replaced isHot with elephant for example, the code would work exactly the same, but a future programmer might wonder what I’d been drinking when I wrote it. :slightly_smiling_face:

Thank you! I shall think on these things. :slight_smile:

1 Like
let isHot = true;
isHot ? console.log("Ouch! That burns!") : console.log("Whew. Glad it wasn't hot!");

can be further simplified to a single console.log()…

console.log(isHot ? `Ouch! That burns!` : `Whew. Glad it wasn't hot!`);
3 Likes

I ran into the same confusion and I think this reply is the best response… From what I’ve gathered, plz correct me if I’m wrong, the reason the first 2 examples work are because they are booleans. They already have a true/false value so the computer can use that value. The string value (and I’m assuming any value that is not boolean) needs something written into the ternary operator to compare to the variable to decide if it is true or false… Is that right? lol

A ternary conditional can take any expression as the condition…

condition ? true_outcome : default_outcome

Expressions can be numbers, strings, comparisons, even a function call. Anything that can be cast to a boolean.

console.log('a' ? 'non-empty string' : 'empty string')
// non-empty string
console.log('' ? 'non-empty string' : 'empty string')
// empty string
console.log(42 ? 'non-zero number' : 'zero')
// non-zero number
console.log(0 ? 'non-zero number' : 'zero')
// zero

I get that all data type are applicable… When you predetermine a variable though, I’m wondering why you have to include the initial “” favoritePhrase === “Love that!” “” The other statements can just determine t/f and the predetermined variables happen to be booleans, but this one requires you to tell it what to argue against the variable, I guess because they aren’t booleans to begin with? Example:

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.’);
-(only “?” is needed)

let favoritePhrase = ‘Love That!’;

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

(here “=== ‘Love That!’” is required… Why?)

To answer OP’s question though, the console will print whatever you tell it to. Not necessarily the data contained in the variable, unless you tell the console to print the variable.

In this instance, the variable favoritePhrase contains the string Love That! , that’s the data inside the variable. The ternary operator only determines if the variable is true or false, then displays whatever you tell it to based on if it’s true or false .

This is referred to as an identity comparison, as in are these two operands exactly alike?

Comparisons are assertions that yield a true or false response.

The above comparison has no shortcut but to face-off each operand with the identity operator. There can be only one result.

Value resolution is how expressions work. isLocked ? ... : ...; contains an expression in the condition of the ternary that resolves to an either/or state, and is easily coerced to a boolean.

The expression could naively be written as a comparison, but given the above rather moot.

isLocked === true ? ... : ...;

It’s not a comparison we need to make given value resolution which does this for us.


Aside

When calling the same function from either branch of a ternary, it is simpler to call the function on the ternary.

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