This data type allows us to store a string of characters.
We can define a string
with:
- Single quotes:
let myProduct = 'Soda'; let doublequotes = 'I can "use" double quotes too'; let quoteInvalid = 'I can't "use" a single quote again';
You can use double quotes inside, but not single quotes again.
- Double quotes:
let myProduct = "Soda"; let singlequote = "I can 'use' single quotation marks too"; let invalidquote = "I can't 'use' double quotation marks again";
You can use single quotes inside, but not double quotes again.
- Using backticks:
let myName = `Frank`;
This way of assigning strings
brings some advantages:
- Declaring multi-line values:
let text = `Never stop learning :) `;
- Concatenate within the same
string
. For this it is necessary to use this dollar sign followed by braces ${}
and write what we want to concatenate inside those braces:
let variableTitle = "TypeScript";let summary = ` title: ${variableTitle}`;
- Also respect the indentation:
`let html= ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body>. .. </body> </html> `;
Contribution created by: Martín Álvarez.
Want to see more contributions, questions and answers from the community?