Ternary operator Need help understanding how to properly utilize return statements



The assignment of this exercise was to return either value depending on the comparison. The statement reads : Next, below the variables, write a conditional statement that checks if the value of checkA is equal to the value checkB . If true, return the result of the callback function. If false, return the string 'inconsistent results' .

The initial code I wrote was posted as follows :

checkA === checkB ? return checkB : return 'inconsistent results' ;

But the correct solution provided to me, was this :

return checkA === checkB ? checkA : 'inconsistent results';

Why is the second code correct but the first code is not ?

I don’t understand what the main difference is.

The thing to understand about return is that it is a statement which may or may not be followed by an expression.

       /-- expression --\
return 'some expression'
\----- statement -----/

A ternary is an expression, so the return would rightly come before it, not within it. Granted, the behavior will be the same in both cases but it loses some of its expressive elegance, so to speak.

return condition ? value : default;
1 Like

I apologize for my delay in response. But thank you for providing your explanation.

In your experience is this the case with all returns?

By that I mean, is the rule that the expression must always come after the return?

Again thank you for your response.

1 Like

The real question might be, what happens when a function has run out of code; and, can we interrupt the code and exit the function?

To the first part, the answer is simple… JS resumes flow from the statement immediately following the function call, or completes any assignment and then resumes. The return value will automatically be, undefined since the function didn’t provide a return value.

To the second part, we already know the answer… Yes, we can interrupt the function anywhere and force a return, with or without a return value. If we only use return the value will be as above, undefined.

In generalized form, the ternary example above could be written,

if (condition) {
    return value
return default

If I understand your question, is it to do with what a function can return? Does it have to be an expression?

To the second part, yes, it must be an expression since if it is a statement that will be executed first (if it doesn’t throw an exception) and the return value will be undefined. An expression is a value, first and foremost.

The value, 'a' is a simple string expression, as is, "", or 0 or 1 a numeric expression. When the return value is any expression then we can trust that it will make it back to the caller.

To the first part, it can return any object, including a function. Please let me know if I’m getting your question correctly.

 > const f = function(x) {
       return console.log(a = x + 5)
<- undefined
 > f(5)
<- undefined
 > const g = function() {
       return x => x + 5
<- undefined
 > g()(5)
<- 10

Note the outcomes.

1 Like

Apologies for the late response.

Just to clarify, statements, which are reruns must always come prior to the expressions themselves.

In regards to the code example you provided, I’m a bit confused on the example.

Is the undefined part the return statement ?

Does both returns still return the outcomes from the console.log ?

1 Like

No, it is the console response. In Chrome the input prompt looks like a greater than, and a response looks like a leftward arrow.

 > // input goes here and then press enter
<- // response will appear here

In the first example, f(5) calls the function f() with an argument of 5. The console.log() is evaluated which includes evaluation of the statement it contains as its argument. a = x + 5 which it then logs to complete the initial eval. Notice that the 10 prints on its own line, as instructed by the function. The return value is what is returned from console.log().

 > f(5)
<- undefined

The 10 is not returned, but printed immediately and it, and x are forgotten.

I appreciate the explanations. I am still very much a beginner and aiming to understand more.

The 10 is printed on a separate line due to the console.log correct ?

I don’t understand why the term “undefined” is listed however. Is that printed on the console as well ?

const f = function(x) {
return console.log(a = x + 5)
← undefined
← undefined

It’s printed before you pass a value the first time. That is because no parameter was inputted correct ?

What I don’t understand is why is it printed to the console log after the 10.