Strings
A string is text. JavaScript has several ways to create and combine strings, with template literals being the most flexible option.
A string is text. In JavaScript you can write strings with single quotes, double quotes, or backticks. Backticks are called template literals. They are usually the best default because they let you embed values directly.
Template literals use backticks and ${expression} interpolation. They also support multi-line strings.
const name = "world";
const greeting = `Hello, ${name}!`;
console.log(greeting); // Hello, world!
const multiline = `Line one
Line two`;
console.log(multiline);In production
Prefer template literals when a string includes values. They keep the expression near the text and avoid long chains of + concatenation.
Enjoyed this? Get more software craft delivered to your inbox.