Thursday, December 8, 2016

Comma operator

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
 expr1, expr2, expr3...

Description
You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.


Examples
for (var i = 0, j = 9; i <= 9; i++, j--)
  console.log("a[" + i + "][" + j + "] = " + a[i][j]);
........................
// Note that the following creates globals and is disallowed in strict mode.
a = b = 3, c = 4; // Returns 4 in console
console.log(a); // 3 (left-most)

x = (y = 5, z = 6); // Returns 6 in console
console.log(x); // 6 (right-most)
........................
function myFunc () {
  var x = 0;
  return (x += 1, x); // the same as return ++x;
} 
@reference_1

No comments:

Post a Comment