Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does assignment with a comma work?

Why does aaa = 1,2,3 work and set the value of aaa to 1?

Why doesn't var bbb = 1,2,3 work?

Why does var bbb = (1,2,3) work and set the value of bbb to 3?

Example console session

like image 420
Shankar Cabus Avatar asked Mar 31 '14 22:03

Shankar Cabus


People also ask

What is the advantage of a comma operator?

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

How do commas work in Python?

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.

What are the uses of comma and conditional (?) Operators?

The comma operator (,) allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates the left operand, then the right operand, and then returns the result of the right operand. First the left operand of the comma operator is evaluated, which increments x from 1 to 2.

What is the purpose of comma operator in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.


3 Answers

There's a lot going on here, but basically, it comes down to the comma operator.

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.


This code:

aaa = 1,2,3

Is equivalent to:

aaa = 1;
2;
3;

So aaa is implicitly declared and assigned a value of 1. Notice that the output on the console is the result of the last statement, 3.


This code:

var bbb = 1,2,3

Is a syntax error because commas in variable declarations are used to declare multiple variables in a single line. As the MDN article points out,

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.

So this code is roughly equivalent to:

var bbb = 1;
var 2;
var 3;

Of course, 2 is not a valid identifier, so it fails at that point.


This code:

var bbb = (1,2,3)

Is very similar to the first, except because the numeric values are wrapped in a parentheses, they are evaluated first. So this is rougly equivalent to:

1;
2;
var bbb = 3;
like image 187
p.s.w.g Avatar answered Oct 22 '22 00:10

p.s.w.g


Comma has multiple uses in Javascript. In the expression:

a = 1, 2, 3;

it's an operator that simply returns its right-hand argument. But it's also part of the syntax of var declarations, which are:

var var1 [ = val1 ], var2 [ = val2 ], var3 [ = val3 ], ...;

(where [...] means that part is optional). Your var declaration is missing the variable names after the commas, so it doesn't parse. You could get the effect you wanted with:

var a = (1, 2, 3);

The parentheses force the commas to be treated as operators rather than delimiters between variable declarations.

like image 9
Barmar Avatar answered Oct 22 '22 00:10

Barmar


In your examples, the comma is used in two contexts:

var statement

The syntax of var statement is:

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];

Here, comma is used to separate variable name-value pairs. The following will not work because a variable name cannot start with a digit (see identifier names):

var bbb = 1, 2, 3;
// SyntaxError: Unexpected number

Comma Operator

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. The following expressions work as follows:

aaa = 1, 2, 3;
  • aaa = 1, 2 yields 2
    • note that aaa = 1 is evaluated first because = has higher priority than ,
  • 2, 3 yields 3
var bbb = (1, 2, 3);
  • the expression (1, 2, 3) yields 3 as described above
  • The variable bbb is assigned the value 3
like image 7
Salman A Avatar answered Oct 22 '22 01:10

Salman A