1

I just saw a expression from a JavaScript sample like following:

  var some = (x, y, z) + a;

what does it mean? and what is the result?

user1484819
  • 899
  • 1
  • 7
  • 18

2 Answers2

6

This is JavaScript's comma operator

x, y, z; // is the same as
x;
y;
z; // this is the last thing returned, so
(x, y, z) === z;

Therefore, var some = (x, y, z) + a; is the same as var some = z + a;, except x and y are evaluated too.

It is useful if you want to shorten things to one line or need something evaluated before a second thing is available.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • can u tell what is the purpose of x and y??is it same as *var x,y;* and *var some=z+a;* – Arun Killu Jan 17 '13 at 03:37
  • @ArunKillu `x` and `y` do not get `var`d, they just get evaluated. You'd normally use an expression rather than just an `x` or `y`, e.g. you invoke a function `setZ()` or do some operator `--z` instead of simply an "`x`". – Paul S. Jan 17 '13 at 03:43
  • @PaulS. yes i got it .i know the use of comma operators in loops and function ,this was a new information.+1 – Arun Killu Jan 17 '13 at 03:50
0

below example shows your solution in this, you see it show only last assign value means (x,y,z) it take only z and add it to 'a' and display it.

<script>
    x=1;
    y=2;
    z=0;
    a=0;
    var some = (x, y, z) + a;
    alert(some)
</script>
Manish Nagar
  • 1,038
  • 7
  • 12