[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Why does (a,b,c) return only c?

Steven D'Aprano

5/25/2015 11:44:00 AM

Consider:

js> var x = (1,2,3);
js> x
3


I expected an error, or an array, or something other than just 3. What's the
explanation for only the last value being returned? I presume it's a
deliberate design feature. Under what circumstances would I use it?

I came across this trying to swap two variables. This was my first attempt,
but I'm afraid that I don't understand what is happening here:

js> var a = 1;
js> var b = 3;
js> a, b = b, a; // swap a and b around
1
js> print(a, b);
1 3


On the swap line, if the right hand side evaluates as only a single value, I
expected any of these behaviours:

(1) an error (can't assign two targets on the left to one on the right);

(2) the "a," on the left gets dropped, and the "b," on the right gets
dropped, so we end up with "b = a" only;

(3) a gets assigned to undefined, and b to 1;

(4) or a to 1 and b to undefined.

Obviously all of my guesses are wrong. What is actually happening there?

This, on the other hand, does appear to work:

js> [a, b] = [b, a];
3,1
js> print(a, b);
3 1

If there any reason not to use that?



--
Steven

21 Answers

Evertjan.

5/25/2015 12:18:00 PM

0

Steven D'Aprano <steve@pearwood.info> wrote on 25 mei 2015 in
comp.lang.javascript:

> Consider:
>
>js> var x = (1,2,3);
>js> x
> 3

Reconsider:

var x = (i=4,++i,i=i*10,a=7,"blah",++i);
alert(x); // 51
alert(i); // 51
alert(a); // 7

var x = (i=4,++i,i=i*10,a=7,"blah",i++);
alert(x); // 50
alert(i); // 51
alert(a); // 7

var x = (i=4,++i,i=i*10,a=7,"blah",++i,"done");
alert(x); // done
alert(i); // 51
alert(a); // 7

So it seems just a set of statements executed in line,
the result value of the last one returned.

Compare this to:

(i=4,++i,i=i*10,a=7,-3,x=i++);
alert(x); // 50
alert(i); // 51
alert(a); // 7

and to:

i=4;++i;i=i*10;a=7;-3;x=i++;
alert(x); // 50
alert(i); // 51
alert(a); // 7

However this errors:

(var i=4,++i,i=i*10,a=7,-3,i++);

Why?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Luuk

5/25/2015 12:42:00 PM

0

On 25-5-2015 14:17, Evertjan. wrote:
> However this errors:
>
> (var i=4,++i,i=i*10,a=7,-3,i++);
>
> Why?
>




You have a syntax error, the 'var' is not allowed there

it is made visible if your line of code is pasted here:
http://www.javascriptlint.com/onlin...


Also:
js> var a=8;2+3;3+4;
7
js> var a=8;2+3,3+4;
7
js> var a=8,2+3,3+4;
typein:10: SyntaxError: missing variable name:
typein:10: var a=8,2+3,3+4;
typein:10: ........^


Christoph M. Becker

5/25/2015 12:58:00 PM

0

Evertjan. wrote:

> Steven D'Aprano <steve@pearwood.info> wrote on 25 mei 2015 in
> comp.lang.javascript:
>
>> Consider:
>>
>> js> var x = (1,2,3);
>> js> x
>> 3

Because ECMAScript implementations don't have a tuple type like Python has.

> So it seems just a set of statements executed in line,
> the result value of the last one returned.

See <http://www.ecma-international.org/ecma-262/5.1/#sec... for details.

> However this errors:
>
> (var i=4,++i,i=i*10,a=7,-3,i++);
>
> Why?

A variable statement is not allowed in an expression, see
<http://www.ecma-international.org/ecma-262/5.1/#se....

--
Christoph M. Becker

Evertjan.

5/25/2015 12:59:00 PM

0

Luuk <luuk@invalid.lan> wrote on 25 mei 2015 in comp.lang.javascript:

>> (var i=4,++i,i=i*10,a=7,-3,i++);
>>
>> Why?
>
> You have a syntax error, the 'var' is not allowed there

Obviously.

Why is this one?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan.

5/25/2015 1:08:00 PM

0

"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 25 mei 2015 in
comp.lang.javascript:

> Evertjan. wrote:
>
>> Steven D'Aprano <steve@pearwood.info> wrote on 25 mei 2015 in
>> comp.lang.javascript:
>>
>>> Consider:
>>>
>>> js> var x = (1,2,3);
>>> js> x
>>> 3
>
> Because ECMAScript implementations don't have a tuple type like Python
> has.
>
>> So it seems just a set of statements executed in line,
>> the result value of the last one returned.
>
> See <http://www.ecma-international.org/ecma-262/5.1/#sec... for
> details.
>
>> However this errors:
>>
>> (var i=4,++i,i=i*10,a=7,-3,i++);
>>
>> Why?
>
> A variable statement is not allowed in an expression, see
> <http://www.ecma-international.org/ecma-262/5.1/#se....

All good points of the "how", thank you.

But what is the intended use of this expression (,,,,,) ?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

5/25/2015 1:32:00 PM

0

Evertjan. wrote:

> But what is the intended use of this expression (,,,,,) ?

It is probably most useful in a for statement, e.g.

for (i = 0, n = array.length; i < n; i++) {
// ...
}

--
Christoph M. Becker

Evertjan.

5/25/2015 3:50:00 PM

0

"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 25 mei 2015 in
comp.lang.javascript:

> Evertjan. wrote:
>
>> But what is the intended use of this expression (,,,,,) ?
>
> It is probably most useful in a for statement, e.g.
>
> for (i = 0, n = array.length; i < n; i++) {
> // ...
> }

No, I tried that, that is not the same,
you cannot exchange (,,;;;) for (,,,) and
(var i=0;;) is allowed, while (var i=0,,,) is not.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Luuk

5/25/2015 5:53:00 PM

0

On 25-5-2015 15:07, Evertjan. wrote:
> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 25 mei 2015 in
> comp.lang.javascript:
>
>> Evertjan. wrote:
>>
>>> Steven D'Aprano <steve@pearwood.info> wrote on 25 mei 2015 in
>>> comp.lang.javascript:
>>>
>>>> Consider:
>>>>
>>>> js> var x = (1,2,3);
>>>> js> x
>>>> 3
>>
>> Because ECMAScript implementations don't have a tuple type like Python
>> has.
>>
>>> So it seems just a set of statements executed in line,
>>> the result value of the last one returned.
>>
>> See <http://www.ecma-international.org/ecma-262/5.1/#sec... for
>> details.
>>
>>> However this errors:
>>>
>>> (var i=4,++i,i=i*10,a=7,-3,i++);
>>>
>>> Why?
>>
>> A variable statement is not allowed in an expression, see
>> <http://www.ecma-international.org/ecma-262/5.1/#se....
>
> All good points of the "how", thank you.
>
> But what is the intended use of this expression (,,,,,) ?
>


an explanation can be found here:
https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma...

some quotes:
"Why use comma operators?
Because they let you specify more than one expression where JavaScript
expects only one. Comma operators are rarely essential but often useful
and just occasionally downright elegant"

"Isn?t the comma operator just a semicolon in disguise?
Semicolons partition statements. Comma operators partition expressions
within statements."

"The comma operator is a close cousin of the && and || operators. All
three operators will return the last expression they evaluate.(*)"
((*), look at the link to get correct conclusion from that... ;)




John-Paul Stewart

5/25/2015 5:59:00 PM

0

On 25/05/15 07:43 AM, Steven D'Aprano wrote:
> Consider:
>
> js> var x = (1,2,3);
> js> x
> 3
>
>
> I expected an error, or an array, or something other than just 3. What's the
> explanation for only the last value being returned? I presume it's a
> deliberate design feature.

Yes, it's a design feature. See, for example:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comm...

"The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand."

> Under what circumstances would I use it?
>
> I came across this trying to swap two variables. This was my first attempt,
> but I'm afraid that I don't understand what is happening here:
>
> js> var a = 1;
> js> var b = 3;
> js> a, b = b, a; // swap a and b around
> 1
> js> print(a, b);
> 1 3
>
>
> On the swap line, if the right hand side evaluates as only a single value, I
> expected any of these behaviours:
>
> (1) an error (can't assign two targets on the left to one on the right);
>
> (2) the "a," on the left gets dropped, and the "b," on the right gets
> dropped, so we end up with "b = a" only;
>
> (3) a gets assigned to undefined, and b to 1;
>
> (4) or a to 1 and b to undefined.
>
> Obviously all of my guesses are wrong. What is actually happening there?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_...

If you read up on operator precedence, you'll see the comma has the
lowest precedence of all operators. So your attempt at swapping is
actually parsed as 3 statements: "a", "b = b", "a". That should make
it clear why the values don't change and why there's no error.

ram

5/25/2015 6:02:00 PM

0

Luuk <luuk@invalid.lan> writes:
>Why use comma operators?

I have my preferred formatting style, which looks like:

return
{ a: 22,
b: 7 };

But JavaScript will not let me use it. I have to use the
comma operator!

return 0,
{ a: 22,
b: 7 };

>>>>>Why does (a,b,c) return only c?

Expressions do not /return/ values, expressions /have/ values.
(More precisely: The /evaluation/ of an expression yields a value.)
/Functions/ return values.