##encode
console.log(unescape(encodeURIComponent("string to be encoded")));
##with escape formatting
console.log(encodeURIComponent("string to be encoded").replace(/%.{2}/g, function(v) { return "\\x" + v.substring(1, 3).toLowerCase(); }));
this will be displayed, formatted with \x for use as escape sequences in various languages