title: decodedURIComponent in javascript (getCookie function)
body: <p>This <code>getCookie</code> function code is from <a href="https://www.w3schools.com/js/js_cookies.asp" rel="nofollow noreferrer">https://www.w3schools.com/js/js_cookies.asp</a></p>
  
  <pre><code>function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i &lt;ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }
  </code></pre>
  
  <p>The part which I cannot understand easily is <code>decodedCookie</code></p>
  
  <pre><code>var decodedCookie = decodeURIComponent(document.cookie);
  </code></pre>
  
  <p>I've already searched for articles about decodeURIComponent,
  and It decodes the <code>encodedURIComponent</code>.</p>
  
  <p>But since theres no encoded cookie, why <code>decodeURIComponent(document.cookie)</code> needed?</p>
  
  <pre><code>function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  }
  </code></pre>
  
  <p>This is the code of <code>setcookie</code> function from w3schools, and they didn<code>t set the cookie with</code>encodeURIComponent`.</p>
  
  <p>So... why? </p>
  
  <p>I need some detailed explanation! Thanks!</p>