# FAQ: Conditional Statements - Ternary Operator

Hi, I have a problem on the third if…else block.
The code ive used is:
favoritePhrase === ‘Love That!’ ? console.log(“I love that!”) : console.log(“I don’t love that!”)

But it wont let me move forward. Can anybody please help!?

Thanks!

I’m sure you have moved on by now but maybe try to end it with a semicolon ‘;’ or / and add let favoritePhrase = ‘Love That!’; before the whole function and / or use single quotes on the first console.log as in ‘I love that!’

I’m finding it a little bit hard to distinguish between the logic of
“Truthy and Falsy Assignments” and “Ternary Operators”. The difference in syntax is clear but I can’t seem to distinguish between the use of both and when to use each.

One clear difference to me is that in Truthy and Falsy Assignment, the variable has no pre-set value. is that the only difference or are there more differences I’m not able to spot?

Thanks for the help in advance.

Consider,

do {
x = Math.floor(Math.random() * 2)
console.log(`\${x} is \${x ? `truthy` : `falsy`}`)
} while (x)
console.log(`\${x} is \${x ? `truthy` : `falsy`}`)

Output

1 is truthy
1 is truthy
1 is truthy
0 is falsy
0 is falsy

Above we examine the truth value of x. This also applies to any expression. All expressions have a truth value. The ternary operator takes an expression or state on the left, and has two branches, one of which will be followed depending upon the truth value of the expression.

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

My entire code. It executes into “I don’t love that!” and fails when I hit run. What is wrong here?

If the ternary operator runs the second expression, you know that the condition is falsy.

You just need to figure out why it’s falsy, and not truthy.

let favoritePhrase = ‘Love that!’;
favoritePhrase === ‘Love That!’

Doesn’t make sense.

First to if/else are edited in the same way. Attempting to edit 3rd if/else in the same manner returns error.

Why is third one different? Why did we redefine value of favoritePhrase when it was already set? Or why is the 3rd typed out differently to first 2? Confusing for noobs.

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

Cheers

The above uses double quotes because there is an apostrophe in the sentence. Single quotes cannot be used in this instance unless we use escapement.

'I don\'t love that!'

The escape tells the parser to treat the apostrophe as a printable character.

Guys, perhaps I am pretty slow, but i do not get AT ALL why the the last part of exercise we need to include:

=== ‘Love That!’

If I delete it the console logs the same result. But in this case we have not assigned it either true or false, so why should it be considered true? I do not understand anything of this exercise.

Hello, @claudiogubitta, and welcome to the forums.

If you delete === 'Love That!', and leave the following intact:

let favoritePhrase = 'Love That!';

console.log(favoritePhrase ? 'I love that!' : "I don't love that!");

You will get the same output:

I love that!

Why? Well, the expression to the left of the ? is evaluated for truthiness. In this case, the variable favoritePhrase is evaluated. favoritePhrase has been assigned to a string literal: 'Love That!'. A non-empty string literal has a boolean value of true, so the value that comes after the ? and before the : is passed as the argument to console.log(), and printed out. If we change the variable assignment to an empty string:

let favoritePhrase = '';

console.log(favoritePhrase ? 'I love that!' : "I don't love that!");

We see the following logged to the console:

I don’t love that!

Why? Again, favoritePhrase is evaluated for truthiness. It is now assigned to an empty string which has a boolean value of false, so the else part of the ternary expression is passed as the argument to console.log(). (The else is represented by :).

Thank you very much, now it is absolutely clear!!!

1 Like