What are template literals in JavaScript?
Asked on Jul 21, 2024
Answer
Template literals in JavaScript are a way to work with strings that allow for embedded expressions and multi-line strings, making them more flexible than regular string literals.
<!-- BEGIN COPY / PASTE -->
const name = "Alice";
const greeting = `Hello, ${name}! Welcome to the JavaScript world.`;
console.log(greeting); // Output: Hello, Alice! Welcome to the JavaScript world.
<!-- END COPY / PASTE -->
Additional Comment:
✅ Answered with JavaScript best practices.- Template literals are enclosed by backticks (``) instead of double or single quotes.
- They allow for string interpolation using the `${expression}` syntax.
- Multi-line strings can be created without needing escape characters for new lines.
- Useful for constructing strings dynamically with embedded expressions.
Recommended Links:
← Back to All Questions