JavaScript Escape Sequences in Strings

Posted by

In JavaScript, you can use escape sequences to add special characters to strings. Here is a list of some common escape sequences:

  • \': Single quote
  • \": Double quote
  • \\: Backslash
  • \n: Newline
  • \r: Carriage return
  • \t: Tab
  • \b: Backspace
  • \f: Form feed

For example:

[dm_code_snippet background="yes" background-mobile="yes" slim="no" line-numbers="no" bg-color="#abb8c3" theme="dark" language="php" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

Copy code
let str = "This is a string\nWith a newline."; console.log(str); // Output: "This is a string // With a newline."

[/dm_code_snippet]

You can also use Unicode escape sequences to represent any Unicode character. For example:

[dm_code_snippet background="yes" background-mobile="yes" slim="no" line-numbers="no" bg-color="#abb8c3" theme="dark" language="php" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

Copy code
let str = "\u00A9"; // Copyright symbol (©) console.log(str); // Output: "©"

[/dm_code_snippet]

Note that you can also use template literals (enclosed in backticks) to include special characters in strings without using escape sequences. For example:

[dm_code_snippet background="yes" background-mobile="yes" slim="no" line-numbers="no" bg-color="#abb8c3" theme="dark" language="php" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

Copy code
let str = `This is a string With a newline.`; console.log(str); // Output: "This is a string // With a newline."

[/dm_code_snippet]

Here are some additional examples of using escape sequences in JavaScript:

[dm_code_snippet background="yes" background-mobile="yes" slim="no" line-numbers="no" bg-color="#abb8c3" theme="dark" language="php" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

Copy code
let str = 'This is a string with a single quote (\') character.'; console.log(str); // Output: "This is a string with a single quote (') character." let str = "This is a string with a double quote (\") character."; console.log(str); // Output: "This is a string with a double quote (") character." let str = "This is a string with a backslash (\\) character."; console.log(str); // Output: "This is a string with a backslash (\) character." let str = "This is a string\twith a tab character."; console.log(str); // Output: "This is a string with a tab character." let str = "This is a string\bwith a backspace character."; console.log(str); // Output: "This is a stringwith a backspace character." let str = "This is a string\fwith a form feed character."; console.log(str); // Output: "This is a string // with a form feed character."

[/dm_code_snippet]

Note that you can use escape sequences not just in string literals, but also in string templates and string concatenation. For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 5;
let y = 10;
let str = `The value of x is ${x}\nThe value of y is ${y}`;
console.log(str);
// Output: "The value of x is 5
//           The value of y is 10"

let str = "The value of x is " + x + "\nThe value of y is " + y;
console.log(str);
// Output: "The value of x is 5
//           The value of y is 10"

[/dm_code_snippet]