tags:

views:

61

answers:

4

The links

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Comma_Operator

says

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

and as an example

for (var i=0, j=9; i <= 9; i++, j--)
  document.writeln("a["+i+"]["+j+"]= " + a[i][j]);

Unable to understand the point exactly. "returns the value of the second operand" - What it means?

Thanks for any help in advance.

+1  A: 

Is as simple as described:

console.log((1,2) == 2); // true

The expression (1,2) will returns the value of the second operand (2).

Edit: The example you post, is not really good to exemplify the 'return value' of the comma operator, since the value of the increment expression on a for loop is not used. That expression is evaluated but nothing is done with its return value.

for ([initialExpression]; [condition]; [incrementExpression])
   statement
CMS
oh but now you're going to get him going on **===** vs **==** :)
cballou
@CMS thanks for your comment. But I can't relate this to the example.
rajakvk
Well, actually I don't need the `===` on this example, since I know both types are Number... :)
CMS
@cballou. lol not exactly...
rajakvk
That's because in the example, the return value of those expressions is irrelevant to how the loop operates. i.e., the return value is not used in any way. The comma operator is just used to execute two expressions without separating them with a semicolon.
Peter Bailey
rajakvk
@CMS the example is not mine, it is posted in MDC
rajakvk
+1  A: 

See the for loop index adjustments

7:~$ js
js> 1,2
2
js> 1,2,3
3
js> 1,2,3,4
4
js>

The idea is that the first expression will be evaluated purely for side-effects such as assignment. The value of the entire expression is the value of the right operand of the , operator.

It is frequently the case that neither of the expressions is being evaluated for its value. In your example case the use of the , operator is to cram two index adjustments into the third expression of the for loop. Neither value is used, it's purely for side effects. Here is a more involved example:

js> i = 10; j = 20;
20
js> t = i++, j--;
20
js> i
11
js> j
19
js> t
10

You can see that both expressions were evaluated (so i and j were bumped) but the value of t is the value of the second expression, j--.

DigitalRoss
@DigitalRoss got it. thanks
rajakvk
A: 
var m = (false, "string");
m === false; // is false
m === "string"' // is true

It actually works for an arbitary number of operands:

var n = (1,2,3,4,5,6);
n === 6; // is true
alunny
Now I got it. ;-)
rajakvk
A: 

I think it's clearer to demonstrate using functions:

console.clear();
function a() {
   console.log('function a executed, its return value is \'ignored\'');
   return 1;
}
function b() {
   console.log('function b executed and expression (a(),b()) will evaluate to the return value from b');
   return 2;
}
console.log((a(),b())==2);

outputs:

function a executed, its return value is 'ignored'
function b executed and expression (a(),b()) will evaluate to the return value from b
true
SBUJOLD