How to Use Six Escape Sequences in JavaScript?

Estimated read time 1 min read

In JavaScript, escape sequences are used to represent special characters that can’t be easily typed into a string. There are several escape sequences in JavaScript, but here are the six most commonly used ones:

  1. \n: A newline character, which can be used to create a line break in a string.
  2. \r: A carriage return character, which can be used to move the cursor to the beginning of the current line.
  3. \t: A tab character, which can be used to insert a tab in a string.
  4. \': A single quote character, which can be used to escape a single quote within a string that’s surrounded by single quotes.
  5. \": A double quote character, which can be used to escape a double quote within a string that’s surrounded by double quotes.
  6. \\: A backslash character, which can be used to escape a backslash within a string.

Here’s an example of how you can use these escape sequences in a JavaScript string:

let str = "Hello\nWorld\r\t\'\"\\";
console.log(str);

This code creates a string that includes several escape sequences, and then logs the string to the console. The output will show the effects of each escape sequence in the string.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply