What does the comma operator do in JavaScript?
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5
evaluates to 5
. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
What does the comma operator (expression, expression) means in Javascript when doing condition evaluation?
My question, what does this expression mean?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand (right most one).
Example:
if (y = f(x), y > x) {
// do something ...
}
This executes first the y = f(x)
which sets up a value for y
(that depends on x
). Then checks if y > x
. If True
then do something
, otherwise do nothing. It is the same as:
y = f(x);
if (y > x) {
// do something ...
}
In your examples:
(true, true & false)
// process `true` (does nothing)
// returns the result of `true & false` => false
(true, true & true)
// process `true` (does nothing)
// returns the result of `true & true` => true
(false, true & false)
// process `false` (does nothing)
// returns the result of `true & false` => false
(false, true & true)
// process `false` (does nothing)
// returns the result of `true & true` => true
Further examples:
(true, true, true, false) // returns false
(false, false, false, true) // returns true
So, in your application code:
if (console.debug(`[NotificationOpenedHandler] Received additionalData:`, e), e.type && e.id) {
Is the same as:
console.debug(`[NotificationOpenedHandler] Received additionalData:`, data);
if(data.type && data.id) {
When is the comma operator useful?
The following is probably not very useful as you don't write it yourself, but a minifier can shrink code using the comma operator. For example:
if(x){foo();return bar()}else{return 1}
would become:
return x?(foo(),bar()):1
The ? :
operator can be used now, since the comma operator (to a certain extent) allows for two statements to be written as one statement.
This is useful in that it allows for some neat compression (39 -> 24 bytes here).
I'd like to stress the fact that the comma in var a, b
is not the comma operator because it doesn't exist within an expression. The comma has a special meaning in var
statements. a, b
in an expression would be referring to the two variables and evaluate to b
, which is not the case for var a, b
.
How does the comma operator work in js?
var c = (a,b);
The above uses the comma operator. It evaluates as the value of its right-hand side (i.e. b
).
var c = a,b;
This does not use the comma operator.
The comma character here forms part of the var
expression which takes a comma-separated list of variables to create in the current scope, each of which can have an optional assignment.
It is equivalent to:
var c = a;
var b;
Comma operator in JavaScript
In this case, the comma separates two variables, and that's it, it's the same as writing
var join = require('path').join;
var pfx = join(__dirname, '../_certs/pfx.p12');
Instead one can do
var join = require('path').join,
pfx = join(__dirname, '../_certs/pfx.p12');
In this case, the comma is just a seperator, much as it would be an object literal or array.
The comma operator, which is only an operator when it acts on two expressions, one on the left side, and one on the right side, can be used when you want to include multiple expressions in a location that requires a single expression.
One example would be in a return statement
[1,2,3].reduce(function(a,b,i) {
return a[i] = b, a; // returns a;
},[]);
etc...
Comma operator returns first value instead of second in argument list?
In the context of a function call, the comma is used to separate parameters from each other. So what you're doing is passing a second parameter to alert()
which gets silently ignored.
What you want is possible this way:
alert((1,2));
The extra brackets form a parameter on their own; inside them you can use the comma as an operator.
What is JavaScript Comma Operator
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/
Javascript: Comma operator, var, and scope - why does it work the way it does?
The commas used in a var
statement are not actually comma operators.
Similarly, the commas you use to separate arguments in a function call are not comma operators either.
How does the comma operator in Javascript work?
This is not an operator. The comma allows you to define more than one variable with a single var
statement:
var nav = $( '#site-navigation' ), // define variable 'nav' and assign $( '#site-navigation' ) to it
button, // this defines variable named 'button' with no initial value
menu; // this defines variable named 'menu' with no initial value
The comma operator is something else - you can read about it here.
Comma operator on defining variables
You are looking in the wrong place - you should be looking at the var
definition:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var
basically, syntax of var
is following:var varname1 [= value1] [, varname2 [= value2] ... [, varnameN [= valueN]]]];
Related Topics
How to Pad a Value With Leading Zeros
How to Split a String, Breaking At a Particular Character
Window.Close and Self.Close Do Not Close the Window in Chrome
Is It Bad Practice to Have a Constructor Function Return a Promise
Addeventlistener Calls the Function Without Me Even Asking It To
How to Get All Properties Values of a JavaScript Object (Without Knowing the Keys)
What Is Destructuring Assignment and Its Uses
How to Check If a String "Startswith" Another String
How May I Sort a List Alphabetically Using Jquery
JavaScript File Upload Size Validation
Short Circuit Array.Foreach Like Calling Break
Why Is the Method Executed Immediately When I Use Settimeout
JavaScript Set Object Key by Variable
All Falsey Values in JavaScript
Load and Execute Order of Scripts