JavaScript: parenthesis surrounding comma separated values [duplicate] - javascript

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

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.

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

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.

Why executing ('Hello','world!) returns 'world!'? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
How does the comma operator work in js? [duplicate]
(1 answer)
Closed 3 years ago.
I'm learning programming with JavaScript, and I just don't know what's happening here:
let myVar = ('Hello','World!');
console.log(myVar);
The output is:
World!
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
This means that the expression 'Hello','World!' evaluates to 'World!'
You're setting the variable to the string "Hello", then setting it to "World". The same thing happens if you set a list of variables inside a parenthetical.
const myVar = (1,2,3,4,5,6);
console.log(myVar);
//6
Where as I think what you were trying to do was use a grouping operator like +.
const myVar = ("Hello, " + "World")
console.log(myVar)
//"Hello, World"

What is JavaScript Comma Operator [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 8 years ago.
What is comma operator in JavaScript. If I execute
var a = 1;
var b = 2;
a,b
I get
2
The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand.
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
More on: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

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