Search code examples
javascriptfirefoxescapingfirefox-developer-tools

JavaScript Escaping Strings don’t work on Firefox Developer Tools


While I was trying the escaping strings on Firefox, I noticed that escaping strings don’t work on Firefox Developer Tools, unless they are inside a console.log() or alert() message.

For example, if you just type inside the console the following message:

"The man whispered, \"please speak to me.\""

the result will be exactly the same with what you typed:

"The man whispered, \"please speak to me.\""

However, if you include the message in console.log(), then it works fine:

Input:

console.log("The man whispered, \"please speak to me.\"");

Output:

The man whispered, "please speak to me."

I don't know if it's some kind of bug or there's something deeper I'm not aware of, so I post it here and I hope someone could elaborate on that.


Solution

  • Firefox's Web Console is pulling double duty as a REPL environment and as the place where window.console dumps its output. When you type '"' in the Web Console, it reads and evaluates that string as a line of Javascript, and gives you the return value in a form it thinks you can use. Since '"' evaluates to a string, Firefox gives you that result as a string literal, "\"". But it still only contains one character, U+0022 QUOTATION MARK. You can see this for yourself with further testing:

    » var str = '"'
    ⬅ undefined
    » str
    ⬅ "\""
    » str.length
    ⬅ 1
    » str.charCodeAt(0)
    ⬅ 34
    

    When you use console.log(), on the other hand, the Web Console is dumping the objects given to console.log as log messages. Firefox has a different idea of what makes a useful log message: for strings, an easy-to-read message is better than a string literal you could copy back into your code. Thus,

    » console.log(str)
      "                                     debugger eval code:1:1
    ⬅ undefined
    

    Note the undefined at the bottom: the Console is still REPLing. The " line is console.log intruding on your conversation.