Convert this Python code to JavaScript code.
arr = [3, 4, 5 ,2 ,6, 8, 1]
let arr = [3, 4, 5, 2, 6, 8, 1];

function bubbleSort(arr) {
  let swapped;
  do {
    swapped = false;
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] && arr[i + 1] && arr[i] > arr[i + 1]) {
        let temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        swapped = true;
      }
    }
  } while (swapped);
  return arr;
}