Blog

- Showing all posts in category: Javascript

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