+ 9
Javascript syntax. What is "let a = (1, 2, 3)"
Can you explain me what "let a = (1, 2, 3)" is in javascript. How it works, for what is used and why console.log(a) outputs 3?
4 Answers
+ 10
As to why you would use it, I don't know. However, I found out what it is. Basically, it allows you to evaluate multiple expressions. The resulting value is the last expression so you get your 3.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
+ 9
The example in site I linked points out using it in a for loop like I've done in C so I would use it there like this reverse of array:
for(i = 0, j = 9; i < 10; i++, j--)
a[i] = b[j];
+ 6
we don't use comma operators in our coding much, but the minifiers use it a lot to minify the js code
best example :
if(x){
foo();
return bar();
}else{
return 1;
}
would minify to
return x?(foo(), bar()): 1
using comma operator
https://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful
+ 1
some more use cases https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/