Obviously, when you create the actual string literal yourself, you will escape the backslash double quote character.
var foo = "baz\"bat";
As with a few other control characters, such as line breaks and backslashes.
var bar = "baz\\bat\nmynew line and a \"quote\" ";
but if you just wrap an existing variable in a quote symbol, that is, you give it to some other system that requires an input quote, there is some confusion.
Obviously, you need to avoid any potential double quotes that are in the string.
var doubleQuoteRe = /\"/g;
var quoted = "\"" + unquoted.replace(escaper, '\\\"') + "\"";
, , . , , . , .