2
<script>
 function makeArray(arg1, arg2){  
        return [ this, arg1, arg2 ];  
    }   
    alert(window.makeArray('one', 'two'));
</script>

Question:

The output of the above script is: [Object Window], one, two, If I changed return [ this, arg1, arg2 ]; to return ( this, arg1, arg2 ); the output is: two. So what is the difference between return[] and return()?

10
  • 1
    @DCoder: Don't be rude. If you're a newbie JavaScript programmer, these things might feel subtle. Commented Jun 13, 2013 at 7:46
  • @LukasEder and yet this should be covered by any decent tutorial Commented Jun 13, 2013 at 7:47
  • 2
    @JanDvorak I don't think I've ever seen a tutorial explain this. Commented Jun 13, 2013 at 7:48
  • 1
    @LukasEder point taken; however, the comma operator is kinda useful when golfing, or in a for head in general: for(i=0, a=[]; i<10; i++) Commented Jun 13, 2013 at 7:52
  • 2
    @JanDvorak the issue here is that the comma operator is well documented, and return is well documented, but no one ever wrote up the bad things that happen if you inadvertently try to combine them. return (a, b) kinda looks like exactly a function call with a list of arguments. Except that it isn't. Commented Jun 13, 2013 at 7:54

5 Answers 5

5

return does not support returning multiple arguments. Nor does it require braces for its argument, so when you call:

return (this, 'one', 'two');

then the braces simply enclose a single expression, where the result of that expression is what's returned to the caller.

Inside that expression you've actually used the "comma operator" (twice).

The comma expression a, b evaluates both a and b, but the result of the expression is just b (i.e. the right hand operand).

In your case you've written (a, b, c), equivalent to ((a, b), c), hence still returning the rightmost operand (i.e. "two")

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

Comments

1

This other question might be useful.

Essentially, if you have return[], really return [], your return value is a list of items. In comparison, return(x) is just another way to call return x.

As mentioned by @Alnitak, in return(a, 'one', 'two') you are returning an expression using the comma operator.

Comments

1

This returns an array with three elements. In Javascript, the [] is for array notation.

return [ this, arg1, arg2 ]; 

Whereas this causes the expression to be evaluated, and the last argument will always be returned (arg2):

return ( this, arg1, arg2 ); 

To deomonstrate:

console.log( ('a', 'b', 'c') ); // c
console.log( ('a', 'b', false) ); // false
console.log( (true, false, 0) ); // 0

Comments

1

There is no special construct like return[] or return(). It's always just return <some expression>.

Square brackets is an array literal, thus you get an array as returned value. () is just parenthesis and x, y is always y (lookup comma operator).

Comments

0

In javascript the first operand that gets solved is the round brackets , ie, () The comma operator resolves to the last operand, ie, 'two' .

return ('one', 'two')
return ('two')

hence it returns two

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.