A little cleaner if you use two counters, one from the start, and the other from the end.
var a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3'];
for (var i = 0, j = a.length-2; i < j; i+=2, j-=2) {
var t1 = a[i],
t2 = a[i+1];
a[i] = a[j];
a[i+1] = a[j+1];
a[j] = t1;
a[j+1] = t2;
}
Or with a little hackery, you can shorten it a little.
var a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3'];
for (var i = 0, j = a.length-2; i < j; i+=2, j-=2) {
a[ i ] = [a[ j ], a[ j ] = a[ i ]][0];
a[i+1] = [a[j+1], a[j+1] = a[i+1]][0];
}
And let's not forget about destructuring assignment. This is coming in ECMAScript 6, but will work right now in Firefox.
var a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3'];
for (var i = 0, j = a.length-2; i < j; i+=2, j-=2) {
[a[i], a[j], a[i+1], a[j+1]] = [a[j], a[i], a[j+1], a[i+1]];
}
One other possibility would be to first reverse the entire array, then reverse the pairs.
var a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3'];
a.reverse();
for (var i = 0; i < a.length; i+=2) {
var t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
Probably slower, but certainly cleaner.
Or using a separate array for the result can eliminate temporary variables.
var a = ['x1', 'y1', 'x2', 'y2', 'x3', 'y3'];
var b = new Array(a.length);
for (var i = 0, j = a.length-2; i <= j; i+=2, j-=2) {
b[j] = a[i],
b[j+1] = a[i+1];
b[i] = a[j];
b[i+1] = a[j+1];
}
Initializing the Array with its full length can give a performance boost in modern implementations.