Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27861 > unrolled thread

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

Started bySteven D'Aprano <steve@pearwood.info>
First post2015-05-25 21:43 +1000
Last post2015-05-26 08:59 -0700
Articles 20 — 9 participants

Back to article view | Back to comp.lang.javascript


Contents

  Why does (a,b,c) return only c? Steven D'Aprano <steve@pearwood.info> - 2015-05-25 21:43 +1000
    Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-25 14:17 +0200
      Re: Why does (a,b,c) return only c? Luuk <luuk@invalid.lan> - 2015-05-25 14:42 +0200
        Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-25 14:58 +0200
      Re: Why does (a,b,c) return only c? "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-25 14:57 +0200
        Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-25 15:07 +0200
          Re: Why does (a,b,c) return only c? "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-25 15:32 +0200
            Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-25 17:50 +0200
          Re: Why does (a,b,c) return only c? Luuk <luuk@invalid.lan> - 2015-05-25 19:53 +0200
            Re: Why does (a,b,c) return only c? "D. Aaron Sawyer" <aaron@110.net> - 2015-05-25 19:49 -0300
    Re: Why does (a,b,c) return only c? John-Paul Stewart <jpstewart@sympatico.ca> - 2015-05-25 13:58 -0400
      Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-25 21:57 +0200
        Re: Why does (a,b,c) return only c? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-25 19:24 -0300
          Re: Why does (a,b,c) return only c? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-26 12:26 +0200
            Re: Why does (a,b,c) return only c? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-26 09:36 -0300
              Re: Why does (a,b,c) return only c? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-26 14:42 +0200
                Re: Why does (a,b,c) return only c? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-26 09:53 -0300
                  Re: Why does (a,b,c) return only c? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-26 15:15 +0200
                  Re: Why does (a,b,c) return only c? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-26 15:17 +0200
            Re: Why does (a,b,c) return only c? Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-26 08:59 -0700

#27861 — Why does (a,b,c) return only c?

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-25 21:43 +1000
SubjectWhy does (a,b,c) return only c?
Message-ID<55630af5$0$13000$c3e8da3$5496439d@news.astraweb.com>
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

[toc] | [next] | [standalone]


#27863

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-25 14:17 +0200
Message-ID<XnsA4A5916ACD1C1eejj99@194.109.133.246>
In reply to#27861
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)

[toc] | [prev] | [next] | [standalone]


#27864

FromLuuk <luuk@invalid.lan>
Date2015-05-25 14:42 +0200
Message-ID<556318ad$0$2868$e4fe514c@news.xs4all.nl>
In reply to#27863
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/online_lint.php


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: ........^

[toc] | [prev] | [next] | [standalone]


#27866

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-25 14:58 +0200
Message-ID<XnsA4A5985881802eejj99@194.109.133.246>
In reply to#27864
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)

[toc] | [prev] | [next] | [standalone]


#27865

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-05-25 14:57 +0200
Message-ID<mjv68c$tft$1@solani.org>
In reply to#27863
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-11.14> 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/#sec-11.1>.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#27867

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-25 15:07 +0200
Message-ID<XnsA4A599E5B8C9Eeejj99@194.109.133.246>
In reply to#27865
"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-11.14> 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/#sec-11.1>.

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)

[toc] | [prev] | [next] | [standalone]


#27868

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-05-25 15:32 +0200
Message-ID<mjv88f$5ef$1@solani.org>
In reply to#27867
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

[toc] | [prev] | [next] | [standalone]


#27869

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-25 17:50 +0200
Message-ID<XnsA4A5B577753DFeejj99@194.109.133.246>
In reply to#27868
"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)

[toc] | [prev] | [next] | [standalone]


#27870

FromLuuk <luuk@invalid.lan>
Date2015-05-25 19:53 +0200
Message-ID<55636189$0$2901$e4fe514c@news.xs4all.nl>
In reply to#27867
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-11.14> 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/#sec-11.1>.
>
> 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-operator/

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... ;)



[toc] | [prev] | [next] | [standalone]


#27876

From"D. Aaron Sawyer" <aaron@110.net>
Date2015-05-25 19:49 -0300
Message-ID<mk08s9$nga$1@dont-email.me>
In reply to#27870
On 2015.05.25 15:01, Stefan Ram wrote:
> 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!
Automatic Semicolon Insertion (ASI) will terminate the return statement as follows:

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

(see, for example, http://www.ecma-international.org/ecma-262/5.1/#sec-7.9 )

The anonymous object with properties a and b is unreachable at execution time.

I am not sure which error is the one you experienced.  If you wish to return
an (anonymous) object, then use

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

>
> 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.
>

[toc] | [prev] | [next] | [standalone]


#27871

FromJohn-Paul Stewart <jpstewart@sympatico.ca>
Date2015-05-25 13:58 -0400
Message-ID<gf4c3c-iks.ln1@mail.binaryfoundry.ca>
In reply to#27861
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/Comma_Operator

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

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.

[toc] | [prev] | [next] | [standalone]


#27874

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-25 21:57 +0200
Message-ID<XnsA4A5DF4D39BAAeejj99@194.109.133.246>
In reply to#27871
John-Paul Stewart <jpstewart@sympatico.ca> wrote on 25 mei 2015 in
comp.lang.javascript: 

> Yes, it's a design feature.  See, for example:
> 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operato
> rs/Comma_Operator 

While this is allowed:

for (var i = 0, j = 9; i <= 9; i++, j--)
  document.write( i + " - " + j + "<br>");

"Note that the comma in the var statement is not the comma operator, because 
it doesn't exist within an expression. Rather, it is a special character in 
var statements to combine multiple of them into one."

After reading that 3 times, I could sort of understand why this is not 
allowed:

for (var i = 0, var j = 9; i <= 9; i++, j--)
  document.write( i + " - " + j + "<br>");

But noy quite.

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

[toc] | [prev] | [next] | [standalone]


#27875

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-05-25 19:24 -0300
Message-ID<mk07ei$t55$1@speranza.aioe.org>
In reply to#27874
On 25/05/2015 16:57, Evertjan. wrote:

> for (var i = 0, j = 9; i <= 9; i++, j--)
>   document.write( i + " - " + j + "<br>");
> 
> "Note that the comma in the var statement is not the comma operator, because 
> it doesn't exist within an expression. Rather, it is a special character in 
> var statements to combine multiple of them into one."

Commas may be either separators such as in variable statements (ES5.1,
§12.2), or operators (ES5.1, §11.14).

> 
> After reading that 3 times, I could sort of understand why this is not 
> allowed:
> 
> for (var i = 0, var j = 9; i <= 9; i++, j--)

The for statement (ES5.1, §12.6.3) has three _optional_ parts:

for ( var VariableDeclarationListNoIn ; Expressionopt ; Expressionopt )

In the first part, we can declare variables separated by commas. In the
last two parts (expressions), we can use comma operators if we wish, but
I think the code becomes cryptic in doing so.

But why on Earth would someone use comma operators? Because they let you
specify more than one expression where JavaScript expects only one. See
examples at:
<https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>


-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#27880

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-26 12:26 +0200
Message-ID<1687513.QYX3roR0lP@PointedEars.de>
In reply to#27875
Joao Rodrigues wrote:

> But why on Earth would someone use comma operators? Because they let you
> specify more than one expression where JavaScript expects only one. See
> examples at:
> <https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>

Bogus.

| //original
| var a = 7, 5;
| //apply = operator
| var a, 5; //a is now 7
| //SyntaxError: missing variable name 

is not a syntax error because “we [haven’t] wrapped the variable assignment 
in parentheses”, but because _“5” is not a valid variable name_.

And the value of “a” is not 7, but _not defined_ at this line (that is 
different from the “undefined” value), because

  var a = 7, 5;

is a syntax error already:

| >>> var a = 7, 5;
| Uncaught SyntaxError: Unexpected number
|   at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
|   at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
|   at Object.InjectedScript.evaluate (<anonymous>:694:21)

The subsequent example in that article, where it works, is

| //original
| var a = (7, 5);
| //apply group
| var a = 5;

where not only the parentheses have been inserted, but magically the comma 
has been replaced by a “=”.  Surprise.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27883

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-05-26 09:36 -0300
Message-ID<mk1pc4$ajd$1@speranza.aioe.org>
In reply to#27880
On 26/05/2015 07:26, Thomas 'PointedEars' Lahn wrote:
> Joao Rodrigues wrote:
> 
>> But why on Earth would someone use comma operators? Because they let you
>> specify more than one expression where JavaScript expects only one. See
>> examples at:
>> <https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>
> 
> Bogus.

A bit of disdain here.

See an example:

var colorIndex = 0,
    colors = ["FF0000", "008000", "FF0086"];

function selectNextColor() {
  return colors[colorIndex++] || colors[colorIndex = 0, colorIndex++];
}

> 
> | //original
> | var a = 7, 5;
> | //apply = operator
> | var a, 5; //a is now 7
> | //SyntaxError: missing variable name 
> 
> is not a syntax error because “we [haven’t] wrapped the variable assignment 
> in parentheses”, but because _“5” is not a valid variable name_.

ACK.

> 
> And the value of “a” is not 7, but _not defined_ at this line (that is 
> different from the “undefined” value), because
> 
>   var a = 7, 5;
> 
> is a syntax error already:


ACK.

> 
> The subsequent example in that article, where it works, is
> 
> | //original
> | var a = (7, 5);
> | //apply group
> | var a = 5;
> 
> where not only the parentheses have been inserted, but magically the comma 
> has been replaced by a “=”.  Surprise.

Wrong, because var a = (7, 5) is evaluated as follows:

var a;
a = (7, 5); // a = 5;

The parentheses (grouping operator, ES 5.1, §11.1.6) return the result
of evaluating expression, i.e (7, 5) will return 5.


-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#27884

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-26 14:42 +0200
Message-ID<2863608.gxWFnX0c9y@PointedEars.de>
In reply to#27883
Joao Rodrigues wrote:

> On 26/05/2015 07:26, Thomas 'PointedEars' Lahn wrote:
>> Joao Rodrigues wrote:
>>> <https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>
>> 
>> Bogus.
> 
> A bit of disdain here.

Pardon?

> See an example:
> […]

Your point being?

>> […]
>> | var a, 5; //a is now 7
          ^
>> | //SyntaxError: missing variable name
                    ^^^^^^^^^^^^^^^^^^^^^
>> is not a syntax error because “we [haven’t] wrapped the variable
>> assignment in parentheses”, but because _“5” is not a valid variable
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> name_.
   ^^^^
> […]
>> The subsequent example in that article, where it works, is
>> […]
>> | var a = 5;
           ^
>> where not only the parentheses have been inserted, but magically the
                                                          ^^^^^^^^^^^^^
>> comma has been replaced by a “=”.  Surprise.
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Wrong, […]

Nonsense.  

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27885

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-05-26 09:53 -0300
Message-ID<mk1qbp$cm3$1@speranza.aioe.org>
In reply to#27884
On 26/05/2015 09:42, Thomas 'PointedEars' Lahn wrote:
> Joao Rodrigues wrote:

>    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> Wrong, […]
> 
> Nonsense.  
> 

Nonsense.

-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#27886

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-26 15:15 +0200
Message-ID<XnsA4A69B3F064eejj99@194.109.133.246>
In reply to#27885
Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 26 mei 2015 in 
comp.lang.javascript:

> On 26/05/2015 09:42, Thomas 'PointedEars' Lahn wrote:
>> Joao Rodrigues wrote:
> 
>>    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> Wrong, […]
>> 
>> Nonsense.  
>> 
> 
> Nonsense.

Seems you two agree!

Noted.


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

[toc] | [prev] | [next] | [standalone]


#27888

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-26 15:17 +0200
Message-ID<1439557.BvG2cW9gDR@PointedEars.de>
In reply to#27885
Joao Rodrigues wrote:

> On 26/05/2015 09:42, Thomas 'PointedEars' Lahn wrote:
>> Joao Rodrigues wrote:
> 
>>    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> Wrong, […]
>> 
>> Nonsense.
>> 
> 
> Nonsense.

Learn to read.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27893

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-05-26 08:59 -0700
Message-ID<cb3e15dc-060b-4d09-868d-ad99dba3db34@googlegroups.com>
In reply to#27880
Thomas 'PointedEars' Lahn wrote:
> Joao Rodrigues wrote:
> 
>> See examples at:
>> <https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/>
> 
> Bogus.

Nonsense.

> | //original
> | var a = 7, 5;
> | //apply = operator
> | var a, 5; //a is now 7
> | //SyntaxError: missing variable name 
> 
> is not a syntax error because "we [haven't] wrapped the variable 
> assignment in parentheses", but because _"5" is not a valid variable 
> name_.

The article make no claim that the syntax error was due to the fact that
the variable was not wrapped in parentheses.  Noting that there is syntax
error in the one without the parentheses is not the same as blaming the
error on their absense.

Instead, the author first introduced a useful "play the parser" technique
with a simpler example, and then applied it to these two putative programs:

    var a = 7, 5;    // 1
    var a = (7, 5);  // 2

Using this (admittedly informal) technique, one can see that (in 1) after
evaluating `a = 7`, the token `5` is out of place.

Of course one can look at §12.2, understand the relevance of

| VariableStatement :
|   var VariableDeclarationList ;
| VariableDeclarationList :
|   VariableDeclaration
|   VariableDeclarationList , VariableDeclaration

and to recognize `a = 7` as meeting `VariableDeclaration`, and then after
several steps to note that `5` is not proper initial character for an
IdentifierName.  This will demontrate the full formal reason for the
syntax error.  But the insight from Angus Croll's informal technique is
quite useful.  It's certainly not "bogus".

> And the value of "a" is not 7, but _not defined_ at this line (that is 
> different from the "undefined" value), because
> 
>   var a = 7, 5;
> 
> is a syntax error already:

Of course it is.  The technique is pedagogical.  By trying to perform the
work the parser might do on a bit of a program, one can often learn how
it might generate an AST, or why it cannot do so.

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web