escape: convert non-ASCII chars to hex string encoding
Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.
encodeURIComponent: encodes everything
decodeURIComponent: converts back
The common way to UTF-8 encode a string is:
How does this work? If we simply did an escape(mystring), then all the spaces would get replaced by %20. We could use encodeURIComponent() to first convert the entire string, but doing a decodeURIComponent would get us back to where we started. To leverage the UTF-8 encoding aspects of URIComponent, we use encodeURIComponent and then use unescape().