Skip to content

JavaScript escape characters | Show escape characters in a string uses the \ (backslash)

  • by

JavaScript uses the \ (backslash) as an escape character for:

  • \’ single quote
  • \” double quote
  • \ backslash
  • \n newline
  • \r carriage return
  • \t tab
  • \b backspace
  • \f form feed
  • \v vertical tab (IE < 9 treats ‘\v’ as ‘v’ instead of a vertical tab (‘\x0B’). If cross-browser compatibility is a concern, use \x0B instead of \v.)
  • \0 null character (U+0000 NULL) (only if the next character is not a decimal digit; else it’s an octal escape sequence).

Note: that the \v and \0 escapes are not allowed in JSON strings.

Examples escape characters in JavaScript

HTML example code.

How can I show escape characters when printing in JavaScript?


str = "Hello\nWorld";



When we want it to give:


Solution: use the \ (backslash) as an escape character.

<!DOCTYPE html>

    str = "Hello \\n World";



Another example of escaping double quotes is special char

    var str = "They call it an \\n \"escape\" character";



Output: They call it an \n “escape” character

Do comment if you have any doubts or suggestions on this is JS char tutorial.

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

Your email address will not be published.

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