encodeURI dùng để mã hóa một URI. encodeURIComponent dùng để tạo param

  • encodeURI() mã hóa các ký tự đặc biệt, ngoại trừ: ~!@#$&*()=:/,;?+
  • encodeURIComponent() mã hóa các ký tự đặc biệt, ngoại trừ: -_.!~*'()
s = "http://www.example.com/string with + and ? and & and spaces";
encodeURI(s)  //http://www.example.com/string%20with%20+%20and%20?%20and%20&%20and%20spaces
encodeURIComponent(s)  //http%3A%2F%2Fwww.example.com%2Fstring%20with%20%2B%20and%20%3F%20and%20%26%20and%20spaces

javascript - What is the difference between decodeURIComponent and decodeURI? - Stack Overflow

Nguồn:: Should I use encodeURI or encodeURIComponent for encoding URLs?

If you’re encoding a string to put in a URL component (a querystring parameter), you should call encodeURIComponent.

If you’re encoding an existing URL, call encodeURI.
Mọi URL đều là URI

/**

 * decodeURI() can't decode the `%` character, as it is used in any encoded character
 * @author [Why does decodeURIComponent('%') lock up my browser?](https://stackoverflow.com/a/54310080/3416774)
 */
export function decodeURIComponentSafe(path: string): string {
  return decodeURIComponent(path.replace(/%(?![0-9a-fA-F]+)/g, "%25"));
}

GitHub - jridgewell/safe-decode-uri-component: Safely decode URI components, leaving invalid sequences as they are

encodeURI nên được đặt tên là fixBrokenURI(), còn decodeURI() nên được đặt là potentiallyBreakMyPreviouslyWorkingURI()