Javascript String Escape Literals

Introduction

The String data type includes several character literals to represent nonprintable characters:

LiteralMeaning
\n New line
\t Tab
\b Backspace
\r Carriage return
\f Form feed
\\ Backslash (\)
\' Single quote ', used when the string is delineated by single quotes. Example: 'He said, \'hey.\''.
\" Double quote ", used when the string is delineated by double quotes. Example: "He said, \"hey.\"".
\` Backtick `, used when the string is delineated by backticks. Example: `He said, \`hey.\``.
\xnn A character represented by hexadecimal code nn (where n is a hexadecimal digit 0-F). Example: \x41 is equivalent to "A".
\unnnn A Unicode character represented by the hexadecimal code nnnn (where n is a hexadecimal digit 0-F).

These character literals can be included anywhere with a string and will be interpreted as if they were a single character:

let text = "This is the letter sigma: \u03a3."; 



PreviousNext

Related