Home > Enterprise >  JavaScript Console Changing String Text
JavaScript Console Changing String Text

Time:12-16

I was just doing stuff in microsoft edge console, and I noticed that when I type in the following:

str = "<script>"

it returns with the value of the string as:

'<\script>'

Why did it add a backslash?

CodePudding user response:

It's formatting it the way you should do it if you were writing the string in a <script> block of an HTML file. In these blocks you should not write <script> and </script> literally, because they'll be parsed as starting or ending the block. The escaped character prevents them from being recognized as those HTML tags.

This is somewhat related to Why split the <script> tag when writing it with document.write()?

  • Related