What is JavaScript Comma Operator [duplicate] - javascript

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/

Related

Why do both [-[]] and [+[]] return [0] in Javascript? [duplicate]

This question already has an answer here:
why a plus sign before array notation in javascript returns zero
(1 answer)
Closed 1 year ago.
Why do both [-[]] and [+[]] return [0] in Javascript?
console.log([+[]])
console.log([-[]])
unary + or - operator just converts it operand to a number. Empty array converted to number is 0. So you got the same result for both [+[]] and [-[]].
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus
https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-unary-plus-operator

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"

Why "Hello" > "World" is returning false [duplicate]

This question already has answers here:
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Closed 4 years ago.
Can anyone explain me why this:
"Hello" > "World"
is returning false?
It compares them char by char, first it compares "H" > "W" and since that's false. It returns false.

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

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