title: Change base of a number in JavaScript using a given digits array
body: <p>I was required to make a method to convert integer from base ten to some another base in JavaScript, and it should also support providing your custom digits array. For example, </p>
  
  <pre><code>toBase(10, 2 ["A","B"])// returns 'BABA'
  </code></pre>
  
  <p>and if digits array is not provided, it should work as JavaScript 'toString' method</p>
  
  <pre><code>var a = 10;
  a.toString(2);//returns '1010'
  </code></pre>
  
  <p>I have wrote a function to convert an integer to another base from base 10 number, with an option of providing digits array - </p>
  
  <pre><code>function toBase(number, radix, digits)  
  {
      radix = radix || 10;
      digits = digits || "0123456789abcdefghijklmnopqrstuvwxyz".split("").slice(0, radix)
  
      if (radix &gt; digits.length) {
          var msg = "Not enough digits to represent the number '" + number + "' in base " + radix;
          throw Error(msg);
      }
  
      if (number === 0) return digits[0];
      var a = []
      while (number) {
          a.splice(0, 0, digits[number % radix])
          number = parseInt(number / radix);
      }
      return a.join("");
  }
  </code></pre>
  
  <p>This function works fine for me, but I want to know if is there any better way to do it? Thanks.</p>