JavaScript Escape Sequences

Escape sequences are also useful for situations where you want to use characters that can’t be typed using a keyboard. Here are some other most commonly used escape sequences.

  • \n is replaced by the newline character
  • \t is replaced by the tab character
  • \r is replaced by the carriage-return character
  • \b is replaced by the backspace character
  • \\ is replaced by a single backslash (\)

Here’s an example to clarify the how escape sequences actually works:

var str1 = "The quick brown fox \n jumps over the lazy dog.";
document.write("<pre>" + str1 + "</pre>"); // Create line break
var str2 = "C:\Users\Downloads";
document.write(str2); // Prints C:UsersDownloads
var str3 = "C:\\Users\\Downloads";
document.write(str3); // Prints C:\Users\Downloads