Why is the return value of the ternary operator ingored?

The ternary-operator lesson from the JavaScript course seems to require the student to write:
condition ? console.log(‘true-sentence’) : console.log(‘false-sentence’)
but the following does seem to work:
console.log(condition ? ‘true-sentence’ : ‘false-sentence’)

The latter is less repetitive and therefore shorter, and it doesn’t ignore the return value of the ternary operator, illustrating why a ternary operator is useful.

Why does the course still prefer (even require) the former?

Can you share the course link here?

There is documentation if you want to know about the Conditional (ternary) operator:

Would this be the sort of link you are referring to?

Thank you for the documentation. It uses the ternary operator in the way I suggested. It doesn’t ignore the return value like the codecademy lesson seems to suggest. But perhaps the course knows something I don’t.

I wondered this myself and think it is because functions and return values are not yet introduced at this point in the course. At this stage they compare the ternary operator to a simple if...else statement which by itself does not have any return value.

If the exercise would make you do a console.log(condition ? ‘true-sentence’ : ‘false-sentence’) it would be comparable to

function ternary(condition, truestatement, falsestatement){
    if(condition) return truestatement;
    if(!condition) return falsestatement;

console.log(ternary(condition, 'true-sentence', 'false-sentence'));

but then again, the reader doesn’t know about functions at this point.

1 Like

Good analogy, but I’d say it’s a bit more like:

function ternary(condition, truestatement, falsestatement){
    if(condition) {
      return truestatement
    } else {
      return falsestatement;

console.log(ternary(condition, 'true-sentence', 'false-sentence'));

Only because a ternary expression requires an else. :wink:

That is the preferred way to write this. console.log() takes any expression, including assignment (which we can for now ignore). The value expressions are easy to read, and maintain. While a ternary supports action code in the branches, it is not a good choice of conditional for that use; an if statement is more appropriate, imho.

Many thanks! I get riumequa’s point that in order to explain the ternary operator’s working as if it were a function, functions would first have to be covered. But by this point in the course, expressions like ‘2 + 3’ have been covered. Couldn’t one explain that such an expression results in a value, and that the ternary operator simply results in whichever value the condition selects?
From what mtf says I gather that JS uses its ternary operator in the same way as other languages I know. Then it would be nice if this course taught its proper use right from the start.

Does anyone know how I suggest this small improvement to codecademy?

Now we’re coming across https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/function-expressions , where the course seems to require the naive

function func(param){
if (param === expectedvalue){
return true
} else {
return false

and rejects the more concise

function func(param){
return param === expectedvalue

Once more I wonder if I’m missing something about JS. I can see why a course would accept the naive form when being concise is not the point. But why specifically reject the concise form?

Sorry if I should’ve started a new topic. Don’t want to clutter the forum.