What is this bracket sequence syntax called in JS? [duplicate] - javascript

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I really don't know what to google to find out the name for this syntax:
(1,2) seems to evaluate to 2
(1,2,3,"cake") seems to evaluate to "cake".
etc
This is sometimes useful in anonymous functions in Array.reduce, where you need to perform a sequence of operations (say an increment) and also return the element on the right.
But where can I read about it and what's it called?

It's just the comma operator: it evaluates each of its operands (from left to right) and returns the value of the last operand.

Related

What exactly is (alert(1),"") in javascript [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I tried doing google gruyeres XSS challenges (http://google-gruyere.appspot.com/part2), and at the stored AJAX XSS challenge they have the following code part for the JSON response:
all <span style=display:none>"
+ (alert(1),"")
+ "</span>your base
The interesting part is: (alert(1),"")
According to the solution provided, the empty string gets returned. According to my testing, the alert(1) still gets exectued.
Is this some sort of function shorthand, or what would this be called in JS?
Why does it execute the alert, but then return the empty string?
Thank you very much for any help!
Best regards,
Rolf
This is the comma operator. The code executes alert(1), discards its return value, then evaluates "". Since this is the last item in the expression, its value is returned, which is empty string.
The tutorial I linked describes it as follows:
The comma operator in JavaScript evaluates each of its operands. It returns the value of the last operand. Add multiple expressions using the comma operator.

Why in Javascript (1,2) equals 2? [duplicate]

This question already has an answer here:
What is JavaScript Comma Operator [duplicate]
(1 answer)
Closed 3 years ago.
I recently discovered that (1,2) equals 2
Same for (1,3,4) => 4 always return the last number
What is this expression exactly? It's not an array, not an object, what is it ?
Code within parentheses is evaluated as an expression. Several expressions can be grouped together by using a comma. Commas are not just syntax for dividing arguments or function parameters, they double as an operator - much like +, -, etc serve multiple purposes, so does ,
In the case you've outlined (1,2) the grouped expressions are obviously 1 and 2. They are both evaluated to themselves, but when expressions are grouped by comma operators, only the last evaluated expression is returned.
This means:
(1,2) == 2
and:
(1,2,3,4) == 4

JavaScript: parenthesis surrounding comma separated values [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I'm trying to get my head around the reason JS picks the last element (8):
var values = (null, 7, null, 8);
console.log(values);
// Output: 8
console.log and alert behaves the same so I assume it's something to do with the language itself.
From the first line of the docs:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
This one also explained beautifully link
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand

How does JavaScript interpret indexing array with array? [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
Why is this not a SyntaxError error (a bad legacy or a purposeful feature)? (A possible duplicate, however I wasn't able to find an answer here.)
Update: Why the contents of the square brackets are treated as an expression?
The first part:
[1,2,4,8]
is interpreted as an array literal. The second part:
[0,1,2,3]
is interpreted as square bracket notation to access a member of the array. The contents of the square brackets are treated as an expression, which is seen as a sequence of comma separated values:
0,1,2,3 // or (0,1,2,3) as an independent expression
That expression returns the last value, so is effectively:
[1,2,4,8][3] // 8

Comma separated number/function in parenthesis in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I read a line from doT.js:
var global = (function(){ return this || (0||eval)('this'); }());
After it was minified:
l=function(){return this||(0,eval)("this")}();
So what is the (0,eval), I mean what does the comma do?
I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.
The comma operator evaluates both and always returns the last. Much like you said.
You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

Categories

Resources