JavaScript escape backslash | Example code

The backslash () is an escape character in JavaScript. The backslash \ is reserved for use as an escape character in JavaScript.

To escape backslash in JavaScript use two backslashes.

\\

Escaping backslash JavaScript through regex example

HTML example code. You have to escape each \ to be \\:

<!DOCTYPE html>
<html>
<body>

  <script>
   let str = 'Hello World "This" is some sample \\ Text \' ';
   console.log(str);

 </script>
</script>
</body>
</html>

Output:

JavaScript escape backslash

Escape in JavaScript

\' single quote
\" double quote
\\ backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed

Do comment if you have any doubts and suggestions on this JS escape code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.