Shuffle an array in JavaScript

If you have an array, and you want to randomly shuffle its elements, you can use the Fisher–Yates shuffle algorithm. In JavaScript, it looks like this:

function shuffle(xs) {
  var i = xs.length, x, rnd;
  while(i) {
    rnd = Math.floor(Math.random() * i);
    i -= 1;
    x = xs[i];
    xs[i] = xs[rnd];
    xs[rnd] = x;