1

What does it mean when variable value in round brackets in js? For example,

let a = (1,2,3);

What does it mean and why console.log(a) output is 3?

What is usage of comma operator in round brackets in variable initialization?

Kate
  • 19
  • 3
  • 1
    Did you tried running this ? your value will always be the last value. in this case it is 3 – Code Maniac Mar 10 '19 at 13:06
  • The round brackets affect nothing. I guess they are there for reading clarity but have no meaning themselves. – VLAZ Mar 10 '19 at 13:06
  • In JavaScript? I think it means the author thought they were in [Python](https://docs.python.org/3/library/stdtypes.html#tuples)... Did you try logging a to find out what the value is? – jonrsharpe Mar 10 '19 at 13:06
  • 2
    comma operator. Returns last element. – trincot Mar 10 '19 at 13:06
  • @trincot yeah, but the question was about the brackets. Still, https://stackoverflow.com/questions/3561043/what-does-a-comma-do-in-javascript-expressions https://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful – VLAZ Mar 10 '19 at 13:08
  • 2
    @VLAZ They're needed here. If you didn't have them, the commas would be separating multiple variable declarations and you'd get a syntax error. – Barmar Mar 10 '19 at 13:08
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Reactgular Mar 10 '19 at 13:08
  • Possible duplicate of [What does this symbol mean in JavaScript?](https://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – str Mar 10 '19 at 13:14
  • @CodeManiacm, i tried and saw that output is 3. I am more interested why it so and in what cases round brackets are used in variable declaration? – Kate Mar 10 '19 at 13:15
  • Possible duplicate of [When is the comma operator useful?](https://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful) – Basel Issmail Mar 10 '19 at 13:16

4 Answers4

4

The parentheses are needed for grouping. In a let statement, commas are normally used to separate multiple variables that are being declared, e.g.

let a = 1, b = 2, c;

which is short for

let a = 1;
let b = 2;
let c;

If you write

let a = 1, 2, 3;

you'll get a syntax error, because after the comma it expects another variable declaration; it's equivalent to:

let a = 1;
let 2;
let 3;

The second and third declarations are clearly wrong, as 2 and 3 are not variable names.

The parentheses indicate that the whole expression 1, 2, 3 is being used to initialize one variable.

The expression 1, 2, 3 uses the Comma operator, which executes each of its subexpressions and returns the last one as its value. It's pretty useless when the subexpressions are all constants, so I assume your code was just a simplified example. Because the way it's written, it's really just equivalent to:

let a = 3;
Barmar
  • 596,455
  • 48
  • 393
  • 495
3

What you have encountered is the comma operator.

Quoting the docs

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

Therefore in your case 1, 2 and 3 is evaluated and 3 is returned and thus assigned to the variable a.

vn-ki
  • 53
  • 3
  • I would like to accept your answer as solution, because it is simple and quotes the docs. But can you also write something about usage of this construction ( comma operator in round brackets in variable initialization) ? – Kate Mar 10 '19 at 13:47
0

This is not variable declaration-specific thing. You can write (1,2,3) anywhere in your JS code and it will always evaluate to 3. The thing is, JavaScript (like many other programming languages, e.g. C) has comma operator, which simply returns last element. The expression (1,2,3) basically looks just like (1+2+3) to JavaScript, except for comma operator is applied instead of addition.

Tigran Saluev
  • 3,025
  • 1
  • 23
  • 36
0

It's call the comma operator:

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

It's a very handy thing to use in JavaScript, because it allows you to run multiple statements as a single value for an arrow function.

Here is an example of a 10x10 identity matrix generated with one line of code. The last comma value is returned as the value.

const m = new Array(9).fill('000000000').map((v,i)=> (v=[...v],v[i]='1',v.join()));
console.log(m);

The same above code as a blocked function would take a lot more lines.

const m = new Array(9).fill('000000000').map((v,i)=> {
   v = [...v];
   v[i] = '1';
   return v.join()
});
console.log(m);
Reactgular
  • 43,331
  • 14
  • 114
  • 176