# Swap javascript array items with each other
Published on 25 January 2016

If you would like to swap the position items in a javascript array use this code:


var items = ["foo", "bar"];

function changeOrder(posA, posB) {
    var itemA = items[posA];
    var itemB = items[posB];

    items[posA] = itemB;
    items[posB] = itemA;
}

changeOrder(0,1);

console.log(items.join(",")) // will output "bar,foo"

JSFiddle example