[Edit]
+
0
-
0
js convert string to utf8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30// Note: JavaScript engine stores string literals in the UTF-16 encoding format. var toUtf8 = function(text) { return unescape(encodeURIComponent(text)); }; // Usage example: const utf16Text = 'I ❤️ JS'; // I ❤️ JS const utf8Text = toUtf8(utf16Text); // I â¤ï¸ JS console.log(utf8Text); // I â¤ï¸ JS // Warning: // // Although unescape() is not strictly deprecated (as in "removed from the Web standards"), // it is defined in Annex B of the ECMA-262 standard, whose introduction states: // // … All of the language features and behaviors specified in this annex have one or more // undesirable characteristics and in the absence of legacy usage would be removed from // this specification. … … Programmers should not use or assume the existence of these // features and behaviors when writing new ECMAScript code. … // // Source: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape // // See also: // https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD