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?
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?
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.
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>