-6
function myFunction() {
    if (condition)
        return a;
    return b;
}

In this case if the condition is true, both a and b will be returned or only a is returned.

5
  • return a tuple, or class Commented Dec 23, 2016 at 11:09
  • only a will be returned as The return statement stops the execution of a function Commented Dec 23, 2016 at 11:09
  • do somthing like that return [a, b]; or like that return { a: a, b: b }; Commented Dec 23, 2016 at 11:10
  • 2
    Stackoverflow is not a compiler for your source code run. Commented Dec 23, 2016 at 11:20
  • For such a trivial question, it was way faster to build a simple test case than asking for an answer here… Commented Dec 23, 2016 at 11:23

4 Answers 4

1

If the condition is true, only a is returned.

No curly brace means only the next statement is exectuted after the if.

Sign up to request clarification or add additional context in comments.

Comments

0

Only the first return is executed, then the flow is returned in the case the if condition is met..

Comments

0

Try it yourself like below in browser console.

(function()
{
var f1= function(condition){
  if(condition>1)
     return "a"
     return "b"
    };  

var result=  f1(3)
console.log(result)

}());

Comments

0

you can return result as array or object
example:

function myFunction()
{
    if (condition){ 
        return [a,b]; 
    }else{ 
        return [b]; 
    } 
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.