1
<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()?

icedwater
  • 4,701
  • 3
  • 35
  • 50
user2294256
  • 1,029
  • 1
  • 13
  • 22
  • 1
    @DCoder: Don't be rude. If you're a newbie JavaScript programmer, these things might feel subtle. – Lukas Eder Jun 13 '13 at 07:46
  • @LukasEder and yet this should be covered by any decent tutorial – John Dvorak Jun 13 '13 at 07:47
  • 2
    @JanDvorak I don't think I've ever seen a tutorial explain this. – Alnitak Jun 13 '13 at 07:48
  • @JanDvorak: I'm coding JavaScript for ages now, and I have never thought about using `(a, b)` as an expression that returns `b` as shown by [Alnitak](http://stackoverflow.com/a/17081762/521799). If there is a "decent tutorial" explaining this, then just create an answer and link to it. – Lukas Eder Jun 13 '13 at 07:49
  • 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++)` – John Dvorak Jun 13 '13 at 07: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. – Alnitak Jun 13 '13 at 07:54
  • @JanDvorak: *however, the comma operator is kinda useful when golfing [...]*: See? It's actually a very good question making people think! :-) – Lukas Eder Jun 13 '13 at 07:59
  • @LukasEder I've been using the comma operator way before this question, but never in a return statement. I'll try to keep that option in mind, but I doubt that it's actually useful to ever use it there: `return a,b` =>`a;return b`. It could be useful in a braceless `if`, but then again it's rarely useful to do something before returning. – John Dvorak Jun 13 '13 at 08:05
  • @LukasEder also, I love that feeling when I kick out all commas and semicolons from my algorithm. – John Dvorak Jun 13 '13 at 08:06
  • @JanDvorak: Well, it's certainly a good obfuscation technique... – Lukas Eder Jun 13 '13 at 08:11

5 Answers5

6

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")

Alnitak
  • 334,560
  • 70
  • 407
  • 495
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.

Community
  • 1
  • 1
icedwater
  • 4,701
  • 3
  • 35
  • 50
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
MrCode
  • 63,975
  • 10
  • 90
  • 112
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).

lqc
  • 7,434
  • 1
  • 25
  • 25
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