Comma operator in Arrow functions

# Biju (10 years ago)

I was looking into Firefox implementation of Arrow functions

And noticed this

function a(){return 1,2}; a();

gives "2"

But with same with Arrow functions

a=()=>1,2;

a();

gives "1"

To get answer "2" in Arrow functions you need parentheses, like

a=()=>(1,2);

a();

Is this expected ?

# Caitlin Potter (10 years ago)

ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.

# Juriy Zaytsev (10 years ago)

Yep, which is why it parses it as (a=()=>1),2 as far as I can see, so "1"

is expected indeed

# Jordan Harband (10 years ago)

To me, a=()=>1,2; looks like statement1,statement2 where statement1

is the arrow function, and statement2 is 2.

# Allen Wirfs-Brock (10 years ago)

On Mar 14, 2015, at 12:37 PM, Caitlin Potter wrote:

ConciseBody is an AssignmentExpression in this case, so I believe the comma is a syntax error.

no, comma is the lowest precedence of any operator.

hence:

a=1,2

parses as:

(a=1),2

and

a=()=>1,2

parses as:

(a=()=>1),2

Finally, note that

console.log(a=1,2)

assigns the value 1 to a, but outputs 2

# Caitlin Potter (10 years ago)

no, comma is the lowest precedence of any operator.

I think you might have misunderstood what I was saying?

ConciseBody -> AssignmentExpression -> comma is not a part of the ConciseBody -> you probably get a SyntaxError unless the arrow function is passed as a parameter to a function

So: var a = (x) => 1, 2; -> syntax error (not a VariableDeclaration)

Is all I was saying.

# Biju (10 years ago)

So: var a = (x) => 1, 2; -> syntax error (not a VariableDeclaration)

Is all I was saying.

hence:

a=1,2

parses as:

(a=1),2

and

a=()=>1,2

parses as:

(a=()=>1),2

Finally, note that

console.log(a=1,2)

assigns the value 1 to a, but outputs 2

Allen

Looks like I can agree with above, b But why in case of

x=1,2;

assigns 2 to x?

# Biju (10 years ago)

Opps...

Looks like I can agree with above, b But why in case of

x=1,2;

assigns 2 to x?

Error, it is assigning 1, ignore previous mail.

# Allen Wirfs-Brock (10 years ago)

On Mar 14, 2015, at 1:00 PM, Caitlin Potter wrote:

no, comma is the lowest precedence of any operator.

I think you might have misunderstood what I was saying?

ConciseBody -> AssignmentExpression -> comma is not a part of the ConciseBody -> you probably get a SyntaxError unless the arrow function is passed as a parameter to a function

So: var a = (x) => 1, 2; -> syntax error (not a VariableDeclaration)

agreed, because the RHS of an Initializer must be an AssignmentStatement.

# Leon Arnott (10 years ago)

On Sun, Mar 15, 2015 at 5:52 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>

wrote:

Finally, note that

console.log(a=1,2)

assigns the value 1 to a, but outputs 2

I think you meant to type console.log((a=1,2)) ?

# Allen Wirfs-Brock (10 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20150314/c7b33703/attachment