+ 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?

24th Mar 2018, 10:33 PM
Vladi Petrov
Vladi Petrov - avatar
4 ответов
+ 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
24th Mar 2018, 11:07 PM
John Wells
John Wells - avatar
+ 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];
24th Mar 2018, 11:35 PM
John Wells
John Wells - avatar
+ 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
24th Mar 2018, 11:17 PM
Morpheus
Morpheus - avatar
25th Mar 2018, 12:05 AM
Mike Choy
Mike Choy - avatar