I don’t understand why the first two ternary expressions are different from the third. I was able to do the first two on my own, but I had trouble with the third. I had to ask them to give me the solution to the third. They use the equal to symbol in the last, but not in the first two equations.
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!”);
isn’t it easier to just to use the log method once and then using a ternary operator determine what should be logged:
console.log(isLocked ? 'You will need a key to open the door.' : 'You will not need a key to open the door.');
why would you need equality for the first two? Boolean values are already true or false. For the third you need a comparison/equality to check, to see if a specific string is matched
so you only use the comparison operators when there isn’t a Boolean Value assigned?
This answer can be broken down into two parts, writing isLocked === true
result in false
(this values are not equal), so you might as well put isLocked
, which is already false. So its just a shorthand
the other part is that we can also put:
example_one = 3;
example_two = "hello world"
if (example_one){
console.log('positive integers are considered true');
}
if (example_two){
console.log('non-empty strings are true');
}
we don’t have Boolean values here, so the language (JS in this case) will see if the values are truthy. Positive integers and non-empty strings are considered true.
ok , thank you! Appreciate the help