title: Converting to Base64 in JavaScript without Deprecated 'Escape' call
body: <p>My name is Festus.</p>
  
  <p>I need to convert strings to and from Base64 in a browser via JavaScript. The topic is covered quite well on this site and on Mozilla, and the suggested solution seems to be along these lines:</p>
  
  <pre><code>function toBase64(str) {
      return window.btoa(unescape(encodeURIComponent(str)));
  }
  
  function fromBase64(str) {
      return decodeURIComponent(escape(window.atob(str)));
  }
  </code></pre>
  
  <p>I did a bit more research and found out that <code>escape()</code> and <code>unescape()</code> are deprecated and should no longer be used. With that in mind, I tried removing calls to the deprecated functions which yields:</p>
  
  <pre><code>function toBase64(str) {
      return window.btoa(encodeURIComponent(str));
  }
  
  function fromBase64(str) {
      return decodeURIComponent(window.atob(str));
  }
  </code></pre>
  
  <p>This seems to work but it begs the following questions:</p>
  
  <p>(1) Why did the originally proposed solution include calls to <code>escape()</code> and <code>unescape()</code>? The solution was proposed prior to deprecation but presumably these functions added some kind of value at the time.</p>
  
  <p>(2) Are there certain edge cases where my removal of these deprecated calls will cause my wrapper functions to fail?</p>
  
  <p>NOTE: There are other, far more verbose and complex solutions on StackOverflow to the problem of string=>Base64 conversion. I'm sure they work just fine but my question is specifically related to this <em>particular</em> popular solution.</p>
  
  <p>Thanks,</p>
  
  <p>Festus</p>