182k views
0 votes
Give the syntax for template literals / string interpolation

1 Answer

3 votes

Final answer:

Template literals in JavaScript use backticks and allow placeholders with ${expression} for inserting variables and expressions into strings. They also support multi-line text and expression evaluation.

Step-by-step explanation:

The syntax for template literals, also known as string interpolation in many programming languages, particularly JavaScript, is characterized by the usage of backticks (`) rather than the traditional single (') or double (") quotes used for strings. To insert expressions or variables into a string, template literals allow you to include placeholders indicated by ${expression}.

An example of a template literal in JavaScript looks like this:

const name = 'Alice';

const greeting = `Hello, ${name}!`;

console.log(greeting); // Outputs: Hello, Alice!

This expression within the curly braces can be any valid JavaScript expression, such as a variable, function call, or mathematical operation. Template literals also support multi-line strings without needing to use newline characters explicitly, and they can evaluate expressions inside placeholders.

The complete question is: Give the syntax for template literals / string interpolation is:

User Levansuper
by
7.5k points